简体   繁体   English

使用unix套接字进行子进程stdin,stdout,stderr

[英]Using unix socket for subprocess stdin, stdout, stderr

I'd like to wrap a game server process in Python so I can use a socket to access the console from other programs. 我想用Python包装游戏服务器进程,以便可以使用套接字从其他程序访问控制台。 I'd like to be able to read stdout, stderr and be able to send in data to stdin to communicate to the server with a socket. 我希望能够读取stdout,stderr并能够将数据发送到stdin以通过套接字与服务器通信。

Thus far I have tried something like this: 到目前为止,我已经尝试过这样的事情:

sock = socket.socket(socket.AF_UNIX)
sock.setblocking(0)
sock.bind('server.sock')
sock.listen(5)
sock.accept()
fd = sock.makefile()

proc = subprocess.Popen(args, stdin=fd, stdout=fd, stderr=fd)

I can connect to the server.sock unix socket, but no data is ever read when I expect the server to have output. 我可以连接到server.sock Unix套接字,但是当我希望服务器具有输出时,不会读取任何数据。

You cannot ignore the return value of sock.accept() : it returns a tuple, the first item of which is the connected socket object. 您不能忽略sock.accept()的返回值:它返回一个元组,其第一项是连接的套接字对象。 It is different from sock , whose only purpose is to accept (possibly several) connections. 它不同于sock ,后者的唯一目的是接受(可能是多个)连接。 You need to call makefile() on this connected socket object instead of on the original sock . 您需要在此连接的套接字对象上而不是原始sock上调用makefile()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM