py3: re-implement the BaseHTTPServer.test() function
authorPulkit Goyal <7895pulkit@gmail.com>
Fri, 15 Jul 2016 23:00:31 +0530
changeset 29565 143d21a7343e
parent 29564 db565a506729
child 29566 075146e85bb6
py3: re-implement the BaseHTTPServer.test() function The function is changed in python 3. So the latest version of function is re-implemented. One can look at https://hg.python.org/cpython/file/3.5/Lib/http/server.py#l1184 and https://hg.python.org/cpython/file/2.7/Lib/BaseHTTPServer.py#l590 to see the change
tests/tinyproxy.py
--- a/tests/tinyproxy.py	Fri Jul 15 12:39:36 2016 -0400
+++ b/tests/tinyproxy.py	Fri Jul 15 23:00:31 2016 +0530
@@ -15,6 +15,7 @@
 __version__ = "0.2.1"
 
 import BaseHTTPServer
+import optparse
 import os
 import select
 import socket
@@ -143,6 +144,19 @@
         a.write(str(os.getpid()) + "\n")
         a.close()
 
+def runserver(port=8000, bind=""):
+    server_address = (bind, port)
+    ProxyHandler.protocol_version = "HTTP/1.0"
+    httpd = ThreadingHTTPServer(server_address, ProxyHandler)
+    sa = httpd.socket.getsockname()
+    print("Serving HTTP on", sa[0], "port", sa[1], "...")
+    try:
+        httpd.serve_forever()
+    except KeyboardInterrupt:
+        print("\nKeyboard interrupt received, exiting.")
+        httpd.server_close()
+        sys.exit(0)
+
 if __name__ == '__main__':
     argv = sys.argv
     if argv[1:] and argv[1] in ('-h', '--help'):
@@ -158,4 +172,13 @@
             del argv[2:]
         else:
             print("Any clients will be served...")
-        BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)
+
+        parser = optparse.OptionParser()
+        parser.add_option('-b', '--bind', metavar='ADDRESS',
+                          help='Specify alternate bind address '
+                               '[default: all interfaces]', default='')
+        (options, args) = parser.parse_args()
+        port = 8000
+        if len(args) == 1:
+            port = int(args[0])
+        runserver(port, options.bind)