Printing out the actual port bound to when using dynamic port allocation would be nice. #76

Closed
rbtcollins opened this Issue Jun 27, 2011 · 2 comments

Comments

Projects
None yet
2 participants
@rbtcollins

Currently with an app using 'app.run()', if you pass 0.0.0.0:0 in as the address to run on, it will successfully run, but incorrectly print that it is listening on port 0.

What needs to happen is a call to sock.getsockname() to get the port bind() allocated, and then print that, rather than the port that was supplied.

@rbtcollins

This comment has been minimized.

Show comment Hide comment
@rbtcollins

rbtcollins Jun 27, 2011

Oh, I should mention: I'm using the web.py in lucid, (0.33 I think).

And I've used this code to workaround this limitation today:

def patch_dynamic_port():
"""Setup better handling of dynamically allocated ports."""
def runsimple(func, server_address=("0.0.0.0", 8080)):
"""
Runs [CherryPy][cp] WSGI server hosting WSGI app func.
The directory static/ is hosted statically.

    [cp]: http://www.cherrypy.org 
    """
    func = web.httpserver.StaticMiddleware(func)
    func = web.httpserver.LogMiddleware(func)

    server = WSGIStartupPrintingServer(server_address, func,
            server_name="localhost")
    # disabled because until start is called the port is not known.
    # print "http://%s:%d/" % server_address 
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
web.httpserver.runsimple = runsimple

from web.wsgiserver import CherryPyWSGIServer
class WSGIStartupPrintingServer(CherryPyWSGIServer):
    """Overridden to print the startup address."""
    def bind(self, family, type, proto=0):
        super(WSGIStartupPrintingServer, self).bind(family, type, proto)
        host, port = self.socket.getsockname()[:2]
        print "http://%s:%d/" % (host, port)

Oh, I should mention: I'm using the web.py in lucid, (0.33 I think).

And I've used this code to workaround this limitation today:

def patch_dynamic_port():
"""Setup better handling of dynamically allocated ports."""
def runsimple(func, server_address=("0.0.0.0", 8080)):
"""
Runs [CherryPy][cp] WSGI server hosting WSGI app func.
The directory static/ is hosted statically.

    [cp]: http://www.cherrypy.org 
    """
    func = web.httpserver.StaticMiddleware(func)
    func = web.httpserver.LogMiddleware(func)

    server = WSGIStartupPrintingServer(server_address, func,
            server_name="localhost")
    # disabled because until start is called the port is not known.
    # print "http://%s:%d/" % server_address 
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
web.httpserver.runsimple = runsimple

from web.wsgiserver import CherryPyWSGIServer
class WSGIStartupPrintingServer(CherryPyWSGIServer):
    """Overridden to print the startup address."""
    def bind(self, family, type, proto=0):
        super(WSGIStartupPrintingServer, self).bind(family, type, proto)
        host, port = self.socket.getsockname()[:2]
        print "http://%s:%d/" % (host, port)
@anandology

This comment has been minimized.

Show comment Hide comment
@anandology

anandology Jan 23, 2012

Owner

Seems to be working with the latest trunk.

Owner

anandology commented Jan 23, 2012

Seems to be working with the latest trunk.

@anandology anandology closed this Jan 23, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment