`OSError:[Errno 9]在Windows上,文件描述符“”与套接字包装不符

2024-09-27 00:15:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为sockets编写一个包装类,以便可以将它用作一个类似于文件的对象,用于管道化到使用subprocess.Popen()创建的进程的stdin和{}。在

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上可以正常工作):

^{pr2}$

Tags: selfcmdnonereadreturndef错误stdin
1条回答
网友
1楼 · 发布于 2024-09-27 00:15:21

根据关于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系统中工作。在

相关问题 更多 >

    热门问题