使用python实现高性能代理服务器 [tornado proxy]

今天跟大家聊下用python实现http代理那些事,首先先描述下他的应用场景,http代理也是正向代理,对于客户端来说,我只需要指定个proxy_server 就可以代理获取数据了。  

至于场景,我这有两个场景会有帮助….

第一个是爬虫代理,我可以在N个公网vps上,部署toproxy http 代理,然后我的主调度器只需要加入proxy server就可以实现同步调用了。

第二个场景是翻墙上网,我可以翻墙去浏览国内无法访问的页面,比如xxx,你懂的.  

代码我已经推送到github.com 和pypi 源了。  有兴趣的朋友可以给我提isssue,我的readme里面提到了todolist,有兴趣的朋友提pullrequest。 


github.com  https://github.com/rfyiamcool/toproxy

pypi              https://pypi.python.org/pypi/toproxy

最近爬虫太凶猛了,标注下tornado http proxy的原文地址.

http://xiaorui.cc/2015/08/21/%E4%BD%BF%E7%94%A8python%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%80%A7%E8%83%BD%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8-tornado-proxy/

介绍

用tornado实现的高性能代理服务器,涵盖了基本的method , 性能方面经过我的测试,他每秒平均在300个访问,如果在tornado加入threadpool逻辑,并发估计会破1k 。

对于https的支持会有些问题,我后期会增强tornado httpclient对于ssl的支持,另外对于高性能方面会加入gevent。

toproxy 更多介绍

原理

你通过http client附加proxy地址访问页面,我通常会解析你的访问,然后我自己再去访问你刚才提交的页面,然后返回你结果。 当然在协议上来说,这虽然不是最高性能的方法,但是最简单有效的方法…. 如果是底层的socket来写,我首先需要解析你的各种各样的header请求,然后还要考虑多任务的模块,或 prefork 或 异步模式, 这都是开发的成本。 我这里是用tornado这异步框架,本身解决了各个流程的堵塞问题,然又用异步的 httpclient,避免了我请求url时的堵塞。

更多的httpclient文档,httpclient 更多文档

安装

方法1:
pip install toproxy

方法2:
依赖tornado 
python setup.py install

直接使用

方法1:
python  -m toproxy/proxy 8888
Starting HTTP proxy on port 8888

方法2:
python toproxy 8888

模块的调用

from toproxy import run_proxy
run_proxy(port, start_ioloop=False)
...
tornado.ioloop.IOLoop.instance().start()

test

curl -vvv -x xiaorui.cc:8888 http://www.google.com

ab -X xiaorui.cc:8888 -c 200 -n 1000 http://www.hao123.com/ 

```
his is ApacheBench, Version 2.3 <Revision: 655654>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.hao123.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        BWS/1.0
Server Hostname:        www.hao123.com
Server Port:            80

Document Path:          /
Document Length:        750380 bytes

Concurrency Level:      200
Time taken for tests:   7.967 seconds
Complete requests:      1000
Failed requests:        10
   (Connect: 0, Receive: 0, Length: 10, Exceptions: 0)
Write errors:           0
Total transferred:      752184936 bytes
HTML transferred:       751671400 bytes
Requests per second:    125.52 [#/sec] (mean)
Time per request:       1593.406 [ms] (mean)
Time per request:       7.967 [ms] (mean, across all concurrent requests)
Transfer rate:          92199.44 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       24   40  94.6     31    1034
Processing:   289 1371 786.8   1223    4028
Waiting:       46   57  27.8     55     663
Total:        317 1411 791.2   1276    4059

Percentage of the requests served within a certain time (ms)
  50%   1276
  66%   1751
  75%   2030
  80%   2208
  90%   2510
  95%   2768
  98%   3187
  99%   3389
 100%   4059 (longest request)
```

todo

  1. 提高toproxy的性能
  2. 加入重试机制
  3. 加入异步回调通知模式
  4. 批量传送


大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

10 Responses

  1. 菜鸟 2017年1月6日 / 下午2:25

    大神可以用[tornado proxy]结合实例应用写写吗,菜鸟的我还不知道怎么套

  2. 赞一个 2015年10月27日 / 上午8:44

    试了下,不错

  3. proeson 2015年9月16日 / 上午8:53

    翻墙还是做个google镜像站靠谱

  4. 开心就好 2015年9月9日 / 上午8:31

    不知道https的可以破么?

  5. orangleliu 2015年8月25日 / 上午10:23

    大神就是大神 。http代理 证书怎么破

  6. 创e 2015年8月25日 / 上午9:41

    大神就是大神,真棒!

  7. 胡阳 2015年8月25日 / 上午9:41

    大神就是大神,真棒!

创e进行回复 取消回复

邮箱地址不会被公开。 必填项已用*标注