In Python 2 and 3k, using wsgi.simple_server.make_server(host, port, app)
does not raise an exception when the port is already in used. Instead, a call to .server_forever()
or .handle_request()
simply blocks until the other port closes and the next connection is incoming.
import wsgiref.simple_server as simple_server
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/html')])
return ["<html><body><p>Hello!</p></body></html>".encode('utf-8')]
def main():
server = simple_server.make_server('', 8901, application)
server.serve_forever()
if __name__ == "__main__":
main()
I would expect an Exception to be raised, since socket.socket.bind()
also raises an exception in this case. Is there a way to determine if the returned HTTPServer
did successfully bind to the specified port?