我正在使用bottlepy开发我的应用程序。使用paste
作为服务器。
from bottle import route, run, template
@route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost',
port=8080,
debug=True,
reloader=True,
server='paste')
如果其他服务之一在同一端口8080
上运行,我收到消息
python /tmp/test.py
Bottle v0.12.8 server starting up (using PasteServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
Traceback (most recent call last):
File "/Users/test/venv/lib/python2.7/site-packages/bottle.py", line 3113, in run
server.run(app)
File "/Users/test/venv/lib/python2.7/site-packages/bottle.py", line 2812, in run
**self.options)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 1310, in serve
request_queue_size=request_queue_size)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 1153, in __init__
request_queue_size=request_queue_size)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 1132, in __init__
request_queue_size=request_queue_size)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 345, in __init__
HTTPServer.__init__(self, server_address, RequestHandlerClass)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 420, in __init__
self.server_bind()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 434, in server_bind
self.socket.bind(self.server_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 48] Address already in use
Bottle v0.12.8 server starting up (using PasteServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
Traceback (most recent call last):
File "/Users/test/venv/lib/python2.7/site-packages/bottle.py", line 3113, in run
server.run(app)
File "/Users/test/venv/lib/python2.7/site-packages/bottle.py", line 2812, in run
**self.options)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 1310, in serve
request_queue_size=request_queue_size)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 1153, in __init__
request_queue_size=request_queue_size)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 1132, in __init__
request_queue_size=request_queue_size)
File "/Users/test/venv/lib/python2.7/site-packages/paste/httpserver.py", line 345, in __init__
HTTPServer.__init__(self, server_address, RequestHandlerClass)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 420, in __init__
self.server_bind()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 434, in server_bind
self.socket.bind(self.server_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 48] Address already in use
Bottle v0.12.8 server starting up (using PasteServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
它继续而不是停止,我可以添加try...catch..
并捕获此异常并终止该过程。但在此之前,我想知道,是否有任何我可以通过的参数,这会自动停止?
如果我设置reloader=False
那么它就可以了。有没有办法让这个适用于reloader=True
?
我看了,reloader
用于自动重新加载,如果文件更改,但是如果端口不可用,那么它还尝试重新启动服务器。
答案 0 :(得分:2)
这个怎么样:
import socket
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
if not is_port_in_use(8080):
run(host='localhost',
port=8080,
debug=True,
reloader=True,
server='paste')
对于python 2.7使用:
from contextlib import closing
import socket
def is_port_in_use(port):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
return s.connect_ex(('localhost', port)) == 0