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

login
register
mail settings
Submitter Pulkit Goyal
Date July 15, 2016, 5:32 p.m.
Message ID <0041e01bef2d15c8d785.1468603924@pulkit-goyal>
Download mbox | patch
Permalink /patch/15894/
State Accepted
Headers show

Comments

Pulkit Goyal - July 15, 2016, 5:32 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@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
Martijn Pieters - July 16, 2016, 4:44 p.m.
On 15 July 2016 at 18:32, Pulkit Goyal <7895pulkit@gmail.com> wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@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

Excellent work! Thanks for doing this. These two patches LGTM now.

> 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)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

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)