[英]`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.