Python基于ThreadingTCPServer創(chuàng)建多線程代理的方法示例
本文實例講述了Python基于ThreadingTCPServer創(chuàng)建多線程代理的方法。分享給大家供大家參考,具體如下:
#coding=utf8
from BaseHTTPServer import BaseHTTPRequestHandler
from SocketServer import ThreadingTCPServer
import gzip
from StringIO import StringIO
import logging
logging.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提交的數(shù)據(jù)
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):
pass
def 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()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python自動錄入ERP系統(tǒng)數(shù)據(jù)
這篇文章主要介紹了Python如何自動錄入ERP系統(tǒng)數(shù)據(jù),用Python解決Excel問題的最佳方法,文章中有詳細(xì)的代碼示例,需要的朋友可以參考閱讀2023-04-04
python 中文件輸入輸出及os模塊對文件系統(tǒng)的操作方法
這篇文章主要介紹了python 中文件輸入輸出及os模塊對文件系統(tǒng)的操作方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08
Python?常用模塊threading和Thread模塊之線程池
這篇文章主要介紹了Python?threading和Thread模塊之線程池,線程池如消費者,負(fù)責(zé)接收任務(wù),并將任務(wù)分配到一個空閑的線程中去執(zhí)行。并不關(guān)心是哪一個線程執(zhí)行的這個任務(wù),具體介紹需要的小伙伴可以參考下面文章詳細(xì)內(nèi)容2022-06-06
python線程鎖(thread)學(xué)習(xí)示例
python thread提供了低級別的、原始的線程以及一個簡單的鎖,下面提供一個python線程線程鎖(thread)學(xué)習(xí)示例,大家參考使用2013-12-12

