tests/tinyproxy.py
changeset 10282 08a0f04b56bd
parent 2337 3f24bc5dee81
child 14093 ce99d887585f
equal deleted inserted replaced
10281:e7d3b509af8b 10282:08a0f04b56bd
    23 
    23 
    24     def handle(self):
    24     def handle(self):
    25         (ip, port) =  self.client_address
    25         (ip, port) =  self.client_address
    26         if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients:
    26         if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients:
    27             self.raw_requestline = self.rfile.readline()
    27             self.raw_requestline = self.rfile.readline()
    28             if self.parse_request(): self.send_error(403)
    28             if self.parse_request():
       
    29                 self.send_error(403)
    29         else:
    30         else:
    30             self.__base_handle()
    31             self.__base_handle()
    31 
    32 
    32     def _connect_to(self, netloc, soc):
    33     def _connect_to(self, netloc, soc):
    33         i = netloc.find(':')
    34         i = netloc.find(':')
    34         if i >= 0:
    35         if i >= 0:
    35             host_port = netloc[:i], int(netloc[i+1:])
    36             host_port = netloc[:i], int(netloc[i + 1:])
    36         else:
    37         else:
    37             host_port = netloc, 80
    38             host_port = netloc, 80
    38         print "\t" "connect to %s:%d" % host_port
    39         print "\t" "connect to %s:%d" % host_port
    39         try: soc.connect(host_port)
    40         try: soc.connect(host_port)
    40         except socket.error, arg:
    41         except socket.error, arg:
    89         ow = []
    90         ow = []
    90         count = 0
    91         count = 0
    91         while 1:
    92         while 1:
    92             count += 1
    93             count += 1
    93             (ins, _, exs) = select.select(iw, ow, iw, 3)
    94             (ins, _, exs) = select.select(iw, ow, iw, 3)
    94             if exs: break
    95             if exs:
       
    96                 break
    95             if ins:
    97             if ins:
    96                 for i in ins:
    98                 for i in ins:
    97                     if i is soc:
    99                     if i is soc:
    98                         out = self.connection
   100                         out = self.connection
    99                     else:
   101                     else:
   102                     if data:
   104                     if data:
   103                         out.send(data)
   105                         out.send(data)
   104                         count = 0
   106                         count = 0
   105             else:
   107             else:
   106                 print "\t" "idle", count
   108                 print "\t" "idle", count
   107             if count == max_idling: break
   109             if count == max_idling:
       
   110                 break
   108 
   111 
   109     do_HEAD = do_GET
   112     do_HEAD = do_GET
   110     do_POST = do_GET
   113     do_POST = do_GET
   111     do_PUT  = do_GET
   114     do_PUT  = do_GET
   112     do_DELETE=do_GET
   115     do_DELETE = do_GET
   113 
   116 
   114 class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
   117 class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
   115                            BaseHTTPServer.HTTPServer): pass
   118                            BaseHTTPServer.HTTPServer): pass
   116 
   119 
   117 if __name__ == '__main__':
   120 if __name__ == '__main__':