gpt4 book ai didi

python - Windows 上带有套接字包装器的 `OSError: [Errno 9] Bad file descriptor`

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:38 24 4
gpt4 key购买 nike

我正在为套接字编写一个包装类,以便我可以将其用作类似文件的对象,用于通过管道传输到使用 创建的进程的 stdinstdout >subprocess.Popen().

def do_task():
global s #The socket
class sockIO():
def __init__(self, s):self.s=s
def write(self, m): self.s.send(m)
def read(self, n=None): return self.s.read() if n is None else self.s.read(n)
def fileno(self): return self.s.fileno()
#stdio=s.makefile('rw')
stdio=sockIO(s)
cmd = subprocess.Popen('cmd', shell=True,
stdout=stdio, stderr=stdio,
stdin=stdio)

我没有使用 socket.makefile() 因为它给出了 io.UnsupportedOperation: fileno 错误,但使用我目前的代码,我收到以下错误在 Windows 上(在 Linux 上工作正常):

Traceback (most recent call last):
File "C:\Users\admin\Desktop\Projects\Python3\client.py", line 65, in <module>
main()
File "C:\Users\admin\Desktop\Projects\Python3\client.py", line 62, in main
receive_commands2()
File "C:\Users\admin\Desktop\Projects\Python3\client.py", line 57, in receive_commands2
stdin=stdio)
File "C:\Python3\lib\subprocess.py", line 914, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Python3\lib\subprocess.py", line 1127, in _get_handles
p2cread = msvcrt.get_osfhandle(stdin.fileno())
OSError: [Errno 9] Bad file descriptor

最佳答案

根据有关 socket.fileno() 的 Python 文档,指出这在 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()). Unix does not have this limitation.

注意:

上面的代码可以在Linux和其他*nix系统上运行。

关于python - Windows 上带有套接字包装器的 `OSError: [Errno 9] Bad file descriptor`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41425673/

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