-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get the port number after starting the server? #290
Comments
I know there is this print on the code: File: F:/Python/lib/site-packages/waitress/__init__.py
12: server = _server(app, **kw)
13: if not _quiet: # pragma: no cover
14: server.print_listen("Serving on http://{}:{}") But this is not useful because is not the user who needs to know the port, but the program. One way to go could be reimplementing the library serve function: import logging
from waitress.server import create_server
class MediaServer(threading.Thread):
def run(self):
if devMode:
# idempotent if logging has already been set up
logging.basicConfig()
self.server = create_server(APP, host="127.0.0.1", port=0)
if devMode:
self.server.print_listen("Serving on http://{}:{}")
self.server.run()
def getPort(self):
return self.server.effective_port |
I would suggest looking at how webtest is doing this with their StopableWsgiServer [1]. Closing this because such questions should go to the mailing list. [1] https://docs.pylonsproject.org/projects/webtest/en/latest/api.html#webtest.http.StopableWSGIServer |
What they do is the same thing I am avoiding to do: def get_free_port():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 0))
ip, port = s.getsockname()
s.close()
ip = os.environ.get('WEBTEST_SERVER_BIND', '127.0.0.1')
return ip, port They get a random port from the system, but this has a concurrency problem because after calling |
@evandrocoan please read the sections General Support and Using Support Wisely. Thanks for your cooperation. |
I use the port address 0 https://stackoverflow.com/questions/1075399/how-to-bind-to-any-available-port(), i.e.,
Then, it should bind to any free port, but how to know which one?
The text was updated successfully, but these errors were encountered: