本文實例講述了Python基于ThreadingTCPServer創建多線程代理的方法。分享給大家供大家參考,具體如下:
#coding=utf8from BaseHTTPServer import BaseHTTPRequestHandlerfrom SocketServer import ThreadingTCPServerimport gzipfrom StringIO import StringIOimport logginglogging.basicConfig(level=logging.DEBUG,        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',        datefmt='%a, %d %b %Y %H:%M:%S',        filename='proxy.log',        filemode='w')class proxyHandler(BaseHTTPRequestHandler):  def do_POST(self):    while True:      try:        path = self.path        if path.split("/")[-1] =="statistics":          #獲取post提交的數據          datas =gzip.GzipFile(fileobj=StringIO(self.rfile.read())).read()          self.wfile.write(datas)          logging.debug(datas)          print datas      except Exception,e:        logging.error(e)      finally:        self.finish()  def do_CONNECT(self):    pass  def do_GET(self):    passdef test():  host='127.0.0.1'  port=8888  try:    server = ThreadingTCPServer((host, port), proxyHandler)    print 'Welcome to the Server HTTP On %s Port %d...' %(host,port)    server.serve_forever()  except KeyboardInterrupt,e:    logging.error(e)    #print '^C received, shutting down server'    server.socket.close()if __name__ == '__main__':  test()更多關于Python相關內容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結》、《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答