Skip to content
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

Closed
evandrocoan opened this issue Apr 11, 2020 · 4 comments
Closed

How to get the port number after starting the server? #290

evandrocoan opened this issue Apr 11, 2020 · 4 comments

Comments

@evandrocoan
Copy link

evandrocoan commented Apr 11, 2020

I use the port address 0 https://stackoverflow.com/questions/1075399/how-to-bind-to-any-available-port(), i.e.,

from waitress import serve
serve(APP, host="127.0.0.1", port=0)

Then, it should bind to any free port, but how to know which one?

  1. https://stackoverflow.com/questions/5085656/how-to-get-the-current-port-number-in-flask
@evandrocoan
Copy link
Author

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

@mmerickel
Copy link
Member

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

@evandrocoan
Copy link
Author

evandrocoan commented Apr 12, 2020

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 get_free_port, the port returned may be allocated by some other application before the waitress server is created.

@stevepiercy
Copy link
Member

@evandrocoan please read the sections General Support and Using Support Wisely. Thanks for your cooperation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants