简体   繁体   中英

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. 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.

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.

You cannot ignore the return value of sock.accept() : it returns a tuple, the first item of which is the connected socket object. It is different from sock , whose only purpose is to accept (possibly several) connections. You need to call makefile() on this connected socket object instead of on the original sock .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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