我正在尝试让 upstart-socket-bridge 将套接字传递给 Python3 应用程序。
有没有使用 Python 2 或 3 的 USB 工作示例?
此示例仅为测试设置。usb 启动 Python3 应用程序,但 Upstart 的文件描述符似乎无效。
# Edited to remove lots of debug lines
import os, traceback, socket
fd = os.environ["UPSTART_FDS"]
# Log the socket connection activity
with open("/tmp/socket-test-outfile", 'w') as outfile:
outfile.write("debug: fd={}\n".format(fd))
try:
s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
s.listen(1)
conn, addr = s.accept()
outfile.write("{}\n".format(conn.recv(1024).decode('UTF-8')))
except:
outfile.write("s error: {}\n".format(traceback.print_exc()))
outfile.close()
到目前为止,所有尝试的 fd = 10。但该 fd 不会生成可用于 accept() 和 read() 的有用套接字。错误输出为 None
>>> import socket
>>> s = socket.fromfd(10, socket.AF_INET, socket.SOCK_STREAM)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/socket.py", line 219, in fromfd
nfd = dup(fd)
OSError: [Errno 9] Bad file descriptor
如何让 upstart-socket-bridge 与 Python 协同工作?