tests/tinyproxy.py
changeset 28646 f452c1cf7a8f
parent 27637 b502138f5faa
child 28773 0023a6e1328f
equal deleted inserted replaced
28645:c226e9b69bac 28646:f452c1cf7a8f
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 
     2 
     3 from __future__ import absolute_import
     3 from __future__ import absolute_import, print_function
     4 
     4 
     5 __doc__ = """Tiny HTTP Proxy.
     5 __doc__ = """Tiny HTTP Proxy.
     6 
     6 
     7 This module implements GET, HEAD, POST, PUT and DELETE methods
     7 This module implements GET, HEAD, POST, PUT and DELETE methods
     8 on BaseHTTPServer, and behaves as an HTTP proxy.  The CONNECT
     8 on BaseHTTPServer, and behaves as an HTTP proxy.  The CONNECT
    48         i = netloc.find(':')
    48         i = netloc.find(':')
    49         if i >= 0:
    49         if i >= 0:
    50             host_port = netloc[:i], int(netloc[i + 1:])
    50             host_port = netloc[:i], int(netloc[i + 1:])
    51         else:
    51         else:
    52             host_port = netloc, 80
    52             host_port = netloc, 80
    53         print "\t" "connect to %s:%d" % host_port
    53         print("\t" "connect to %s:%d" % host_port)
    54         try: soc.connect(host_port)
    54         try: soc.connect(host_port)
    55         except socket.error as arg:
    55         except socket.error as arg:
    56             try: msg = arg[1]
    56             try: msg = arg[1]
    57             except (IndexError, TypeError): msg = arg
    57             except (IndexError, TypeError): msg = arg
    58             self.send_error(404, msg)
    58             self.send_error(404, msg)
    68                                  " 200 Connection established\r\n")
    68                                  " 200 Connection established\r\n")
    69                 self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
    69                 self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
    70                 self.wfile.write("\r\n")
    70                 self.wfile.write("\r\n")
    71                 self._read_write(soc, 300)
    71                 self._read_write(soc, 300)
    72         finally:
    72         finally:
    73             print "\t" "bye"
    73             print("\t" "bye")
    74             soc.close()
    74             soc.close()
    75             self.connection.close()
    75             self.connection.close()
    76 
    76 
    77     def do_GET(self):
    77     def do_GET(self):
    78         (scm, netloc, path, params, query, fragment) = urlparse.urlparse(
    78         (scm, netloc, path, params, query, fragment) = urlparse.urlparse(
    93                 for key_val in self.headers.items():
    93                 for key_val in self.headers.items():
    94                     soc.send("%s: %s\r\n" % key_val)
    94                     soc.send("%s: %s\r\n" % key_val)
    95                 soc.send("\r\n")
    95                 soc.send("\r\n")
    96                 self._read_write(soc)
    96                 self._read_write(soc)
    97         finally:
    97         finally:
    98             print "\t" "bye"
    98             print("\t" "bye")
    99             soc.close()
    99             soc.close()
   100             self.connection.close()
   100             self.connection.close()
   101 
   101 
   102     def _read_write(self, soc, max_idling=20):
   102     def _read_write(self, soc, max_idling=20):
   103         iw = [self.connection, soc]
   103         iw = [self.connection, soc]
   120                         break
   120                         break
   121                     if data:
   121                     if data:
   122                         out.send(data)
   122                         out.send(data)
   123                         count = 0
   123                         count = 0
   124             else:
   124             else:
   125                 print "\t" "idle", count
   125                 print("\t" "idle", count)
   126             if count == max_idling:
   126             if count == max_idling:
   127                 break
   127                 break
   128 
   128 
   129     do_HEAD = do_GET
   129     do_HEAD = do_GET
   130     do_POST = do_GET
   130     do_POST = do_GET
   140         a.close()
   140         a.close()
   141 
   141 
   142 if __name__ == '__main__':
   142 if __name__ == '__main__':
   143     from sys import argv
   143     from sys import argv
   144     if argv[1:] and argv[1] in ('-h', '--help'):
   144     if argv[1:] and argv[1] in ('-h', '--help'):
   145         print argv[0], "[port [allowed_client_name ...]]"
   145         print(argv[0], "[port [allowed_client_name ...]]")
   146     else:
   146     else:
   147         if argv[2:]:
   147         if argv[2:]:
   148             allowed = []
   148             allowed = []
   149             for name in argv[2:]:
   149             for name in argv[2:]:
   150                 client = socket.gethostbyname(name)
   150                 client = socket.gethostbyname(name)
   151                 allowed.append(client)
   151                 allowed.append(client)
   152                 print "Accept: %s (%s)" % (client, name)
   152                 print("Accept: %s (%s)" % (client, name))
   153             ProxyHandler.allowed_clients = allowed
   153             ProxyHandler.allowed_clients = allowed
   154             del argv[2:]
   154             del argv[2:]
   155         else:
   155         else:
   156             print "Any clients will be served..."
   156             print("Any clients will be served...")
   157         BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)
   157         BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)