停止生成的进程继承绑定端口
Stop spawned process from inherting bound port
我正在编写一个小型 HTTP 服务器,它根据请求生成一个 VLC 实例。
但是,如果我停止我的服务器并尝试在启动的 VLC 实例仍然是 运行 时重新启动它,它会失败并显示 error: [Errno 48] Address already in use
服务器:
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
vlcPort = 8191
linkArg = "example.mp4"
os.system('~/scripts/backVlcNoBorder.sh --http-port %s %s'%(vlcPort,linkArg))
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
def server_bind(self):
print "self.allow_reuse_address",self.allow_reuse_address
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
# HTTPServer.server_bind(self)
if __name__ == "__main__":
server = ThreadingHTTPServer(("localhost",1111), Handler)
server.serve_forever()
启动VLC调用的脚本(~/scripts/backVlcNoBorder.sh)
#!/bin/bash
nohup /Applications/VLC.app/Contents/MacOS/VLC $@ >/dev/null 2>/dev/null &
如果我向服务器发出请求,VLC 就会启动。
如果我然后关闭服务器,但不是 VLC,并尝试重新启动服务器,它会失败:
self.allow_reuse_address 1
Exception in thread Thread-1:
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "./http_request_server.py", line 263, in serve_on_port
server = ThreadingHTTPServer(("localhost",1111), Handler)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 420, in __init__
self.server_bind()
File "./http_request_server.py", line 256, in server_bind
self.socket.bind(self.server_address)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 48] Address already in use
之后我 运行 lsof -n -i4TCP:1111 | grep LISTEN
看看什么仍然绑定到 1111
bash 24365 admin 3u IPv4 0x6d73e857a76dcad5 0t0 TCP 127.0.0.1:lmsocialserver (LISTEN)
VLC 24366 admin 3u IPv4 0x6d73e857a76dcad5 0t0 TCP 127.0.0.1:lmsocialserver (LISTEN)
我不明白为什么 bash 和 VLC 都在监听 1111,我想这与 os.system
和使用 fork()
可能有关?
如何在不关闭 VLC 的情况下重新启动服务器,或者如何阻止 VLC 继承绑定端口?
OS 是 Mac OSX 10.9
有两个问题:
- 使用
subprocess
并通过close_fds=True
修复第一个;如果 http 服务器进程以外的其他进程保持 fd 打开
- 配置您的服务器以使用
SO_REUSEADDR
or its analog on your system 如果您重新启动速度太快(TIME_WAIT
状态)。
我正在编写一个小型 HTTP 服务器,它根据请求生成一个 VLC 实例。
但是,如果我停止我的服务器并尝试在启动的 VLC 实例仍然是 运行 时重新启动它,它会失败并显示 error: [Errno 48] Address already in use
服务器:
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
vlcPort = 8191
linkArg = "example.mp4"
os.system('~/scripts/backVlcNoBorder.sh --http-port %s %s'%(vlcPort,linkArg))
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
def server_bind(self):
print "self.allow_reuse_address",self.allow_reuse_address
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
# HTTPServer.server_bind(self)
if __name__ == "__main__":
server = ThreadingHTTPServer(("localhost",1111), Handler)
server.serve_forever()
启动VLC调用的脚本(~/scripts/backVlcNoBorder.sh)
#!/bin/bash
nohup /Applications/VLC.app/Contents/MacOS/VLC $@ >/dev/null 2>/dev/null &
如果我向服务器发出请求,VLC 就会启动。 如果我然后关闭服务器,但不是 VLC,并尝试重新启动服务器,它会失败:
self.allow_reuse_address 1
Exception in thread Thread-1:
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "./http_request_server.py", line 263, in serve_on_port
server = ThreadingHTTPServer(("localhost",1111), Handler)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 420, in __init__
self.server_bind()
File "./http_request_server.py", line 256, in server_bind
self.socket.bind(self.server_address)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 48] Address already in use
之后我 运行 lsof -n -i4TCP:1111 | grep LISTEN
看看什么仍然绑定到 1111
bash 24365 admin 3u IPv4 0x6d73e857a76dcad5 0t0 TCP 127.0.0.1:lmsocialserver (LISTEN)
VLC 24366 admin 3u IPv4 0x6d73e857a76dcad5 0t0 TCP 127.0.0.1:lmsocialserver (LISTEN)
我不明白为什么 bash 和 VLC 都在监听 1111,我想这与 os.system
和使用 fork()
可能有关?
如何在不关闭 VLC 的情况下重新启动服务器,或者如何阻止 VLC 继承绑定端口?
OS 是 Mac OSX 10.9
有两个问题:
- 使用
subprocess
并通过close_fds=True
修复第一个;如果 http 服务器进程以外的其他进程保持 fd 打开 - 配置您的服务器以使用
SO_REUSEADDR
or its analog on your system 如果您重新启动速度太快(TIME_WAIT
状态)。