Python基于ThreadingTCPServer创建多线程代理的方法示例


Posted in Python onJanuary 11, 2018

本文实例讲述了Python基于ThreadingTCPServer创建多线程代理的方法。分享给大家供大家参考,具体如下:

#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提交的数据
          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()

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python抓取网页图片示例(python爬虫)
Apr 27 Python
python实现上传样本到virustotal并查询扫描信息的方法
Oct 05 Python
基于python list对象中嵌套元组使用sort时的排序方法
Apr 18 Python
TensorFlow损失函数专题详解
Apr 26 Python
python 3调用百度OCR API实现剪贴板文字识别
Sep 04 Python
在python中按照特定顺序访问字典的方法详解
Dec 14 Python
Python3 mmap内存映射文件示例解析
Mar 23 Python
Python进程的通信Queue、Pipe实例分析
Mar 30 Python
Flask中sqlalchemy模块的实例用法
Aug 02 Python
python+appium+yaml移动端自动化测试框架实现详解
Nov 24 Python
python 爬虫之selenium可视化爬虫的实现
Dec 04 Python
python执行js代码的方法
May 13 Python
python实现员工管理系统
Jan 11 #Python
Python使用base64模块进行二进制数据编码详解
Jan 11 #Python
Python实现备份MySQL数据库的方法示例
Jan 11 #Python
教你用Python写安卓游戏外挂
Jan 11 #Python
python实现学生管理系统
Jan 11 #Python
linecache模块加载和缓存文件内容详解
Jan 11 #Python
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
Jan 11 #Python
You might like
simplehtmldom Doc api帮助文档
2012/03/26 PHP
thinkphp多层MVC用法分析
2015/12/30 PHP
Windows平台实现PHP连接SQL Server2008的方法
2017/07/26 PHP
原生php实现excel文件读写的方法分析
2018/04/25 PHP
PHP堆栈调试操作简单示例
2018/06/15 PHP
Add a Formatted Table to a Word Document
2007/06/15 Javascript
基于jQuery的仿flash的广告轮播
2010/11/05 Javascript
JavaScript中数组的排序、乱序和搜索实现代码
2011/11/30 Javascript
jQuery中对节点进行操作的相关介绍
2013/04/16 Javascript
jquery indexOf使用方法
2013/08/19 Javascript
jquery链式操作的正确使用方法
2014/01/06 Javascript
动态创建script在IE中缓存js文件时导致编码的解决方法
2014/05/04 Javascript
css如何让浮动元素水平居中
2015/08/07 Javascript
JavaScript基础篇(6)之函数表达式闭包
2015/12/11 Javascript
JavaScript和jquery获取父级元素、子级元素、兄弟元素的方法
2016/06/05 Javascript
jQuery的中 is(':visible') 解析及用法(必看)
2017/02/12 Javascript
微信小程序 PHP生成带参数二维码
2017/02/21 Javascript
javascript兼容性(实例讲解)
2017/08/15 Javascript
css配合JavaScript实现tab标签切换效果
2018/10/11 Javascript
解决Vue大括号字符换行踩的坑
2020/11/09 Javascript
python实现通过shelve修改对象实例
2014/09/26 Python
浅谈python中的占位符
2017/11/09 Python
解决python3 json数据包含中文的读写问题
2018/05/10 Python
儿童python练习实例
2018/05/27 Python
Django视图、传参和forms验证操作
2020/07/15 Python
pycharm 如何取消连按两下shift出现的全局搜索
2021/01/15 Python
CSS3圆角边框和边界图片效果实例
2016/07/01 HTML / CSS
美国专注于健康商品的网站:eVitamins
2017/01/23 全球购物
Sneaker Studio法国:购买运动鞋
2018/06/08 全球购物
火山动力Java笔试题
2014/06/26 面试题
施工材料员岗位职责
2014/02/12 职场文书
实践单位评语
2014/04/26 职场文书
怀念母亲教学反思
2014/04/28 职场文书
财务工作犯错检讨书
2014/10/07 职场文书
学校党委干部个人对照检查材料思想汇报
2014/10/09 职场文书
导游词之日本富士山
2020/01/06 职场文书