comparison tests/tinyproxy.py @ 29565:143d21a7343e

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
author Pulkit Goyal <7895pulkit@gmail.com>
date Fri, 15 Jul 2016 23:00:31 +0530
parents 33770d2b6cf9
children 075146e85bb6
comparison
equal deleted inserted replaced
29564:db565a506729 29565:143d21a7343e
13 """ 13 """
14 14
15 __version__ = "0.2.1" 15 __version__ = "0.2.1"
16 16
17 import BaseHTTPServer 17 import BaseHTTPServer
18 import optparse
18 import os 19 import os
19 import select 20 import select
20 import socket 21 import socket
21 import sys 22 import sys
22 23
141 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs) 142 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
142 a = open("proxy.pid", "w") 143 a = open("proxy.pid", "w")
143 a.write(str(os.getpid()) + "\n") 144 a.write(str(os.getpid()) + "\n")
144 a.close() 145 a.close()
145 146
147 def runserver(port=8000, bind=""):
148 server_address = (bind, port)
149 ProxyHandler.protocol_version = "HTTP/1.0"
150 httpd = ThreadingHTTPServer(server_address, ProxyHandler)
151 sa = httpd.socket.getsockname()
152 print("Serving HTTP on", sa[0], "port", sa[1], "...")
153 try:
154 httpd.serve_forever()
155 except KeyboardInterrupt:
156 print("\nKeyboard interrupt received, exiting.")
157 httpd.server_close()
158 sys.exit(0)
159
146 if __name__ == '__main__': 160 if __name__ == '__main__':
147 argv = sys.argv 161 argv = sys.argv
148 if argv[1:] and argv[1] in ('-h', '--help'): 162 if argv[1:] and argv[1] in ('-h', '--help'):
149 print(argv[0], "[port [allowed_client_name ...]]") 163 print(argv[0], "[port [allowed_client_name ...]]")
150 else: 164 else:
156 print("Accept: %s (%s)" % (client, name)) 170 print("Accept: %s (%s)" % (client, name))
157 ProxyHandler.allowed_clients = allowed 171 ProxyHandler.allowed_clients = allowed
158 del argv[2:] 172 del argv[2:]
159 else: 173 else:
160 print("Any clients will be served...") 174 print("Any clients will be served...")
161 BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer) 175
176 parser = optparse.OptionParser()
177 parser.add_option('-b', '--bind', metavar='ADDRESS',
178 help='Specify alternate bind address '
179 '[default: all interfaces]', default='')
180 (options, args) = parser.parse_args()
181 port = 8000
182 if len(args) == 1:
183 port = int(args[0])
184 runserver(port, options.bind)