[英]`OSError: [Errno 9] Bad file descriptor` with socket wrapper on Windows
我正在为套接字编写包装器类,因此可以将其用作类似于文件的对象,用于管道传输到由subprocess.Popen()
创建的进程的stdin
和stdout
中。
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文档 :
socket.fileno()
返回套接字的文件描述符(一个小整数)。 这对于select.select()很有用。
在Windows下,不能在可以使用文件描述符的地方使用此方法返回的小整数(例如
os.fdopen()
) 。 Unix没有此限制。
注意:
上面的代码将在Linux和其他* nix系统上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.