[PATCH 1 of 2] py3: re-implement the BaseHTTPServer.test() function

Pulkit Goyal 7895pulkit at gmail.com
Fri Jul 15 13:32:04 EDT 2016


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1468603831 -19800
#      Fri Jul 15 23:00:31 2016 +0530
# Node ID 0041e01bef2d15c8d7852c83d85a5a62d39e6bd1
# Parent  e5b4d79a9140c3d90e9b6aa22070351b73ef2d4c
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

diff -r e5b4d79a9140 -r 0041e01bef2d tests/tinyproxy.py
--- a/tests/tinyproxy.py	Sat May 21 15:23:21 2016 +0900
+++ 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)


More information about the Mercurial-devel mailing list