diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-06-07 10:17:30 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-06-07 10:17:30 +1200 |
commit | b5a74a26ee6548b493cdece5a05f4fcba71c0012 (patch) | |
tree | 6244c184de39e548e38dc63d2f919e0610a02d56 | |
parent | 049d3d2b45d42245a305bd0bd3b5164a44dc7ba2 (diff) | |
download | mitmproxy-b5a74a26ee6548b493cdece5a05f4fcba71c0012.tar.gz mitmproxy-b5a74a26ee6548b493cdece5a05f4fcba71c0012.tar.bz2 mitmproxy-b5a74a26ee6548b493cdece5a05f4fcba71c0012.zip |
Let Pathod pick an arbitrary empty port if -p 0 is specified.
-rw-r--r-- | libpathod/pathod.py | 19 | ||||
-rwxr-xr-x | pathod | 8 |
2 files changed, 21 insertions, 6 deletions
diff --git a/libpathod/pathod.py b/libpathod/pathod.py index ca24c62c..6b3deaa6 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -205,11 +205,24 @@ class PathodApp(tornado.web.Application): # begin nocover -def run(application, port, ssl_options): +def make_server(application, port, address, ssl_options): + """ + Returns the bound port. This will match the passed port, unless the + passed port was 0. In that case, an arbitrary empty port will be bound + to, and this new port will be returned. + """ http_server = tornado.httpserver.HTTPServer( application, ssl_options=ssl_options ) - http_server.listen(port) - tornado.ioloop.IOLoop.instance().start() + http_server.listen(port, address) + port = port + for i in http_server._sockets.values(): + sn = i.getsockname() + if sn[0] == address: + port = sn[1] + return port + +def run(): + tornado.ioloop.IOLoop.instance().start() @@ -5,7 +5,8 @@ import tornado.ioloop if __name__ == "__main__": parser = argparse.ArgumentParser(description='Process some integers.') - parser.add_argument("-p", dest='port', default=9999, type=int, help='Port.') + parser.add_argument("-p", dest='port', default=9999, type=int, help='Port. Specify 0 to pick an arbitrary empty port.') + parser.add_argument("-l", dest='address', default="0.0.0.0", type=str, help='Listening address.') parser.add_argument( "-a", dest='anchors', default=[], type=str, action="append", help='Add an anchor. Specified as a string with the form pattern=pagespec' @@ -48,8 +49,9 @@ if __name__ == "__main__": ) else: ssl = None - print "%s listening on port %s"%(version.NAMEVERSION, args.port) try: - pathod.run(application, args.port, ssl) + port = pathod.make_server(application, args.port, args.address, ssl) + print "%s listening on port %s"%(version.NAMEVERSION, port) + pathod.run() except KeyboardInterrupt: pass |