-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (55 loc) · 1.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 Peng Liu <liupeng@imscv.com>
#
# Distributed under terms of the GNU GPL3 license.
"""
"""
import tornado.ioloop
import tornado.web
import requests
import random
from KuaidailiProxyGenerator import KuaidailiProxyGenerator
from XicidailiProxyGenerator import XicidailiProxyGenerator
class MainHandler(tornado.web.RequestHandler):
def get(self):
used_proxy = random.choice(k.get().values()).data_dict['hash']
print 'Used proxy: %s' % used_proxy
b = requests.get('http://www.baidu.com',
proxies={'http': 'http://' + used_proxy})
self.write(b.text.encode('utf-8'))
class BookHandler(tornado.web.RequestHandler):
"""handle book page."""
def get(self, bookid):
"""TODO: to be defined1. """
used_proxy = random.choice(k.get().values())
print used_proxy
used_proxy = used_proxy.data_dict['hash']
print 'Used proxy: %s' % used_proxy
b = requests.get('http://book.douban.com/subject/%s' % bookid,
proxies={'http': 'http://' + used_proxy})
self.write(b.text.encode('utf-8'))
class LinkHandler(tornado.web.RequestHandler):
"""handle url."""
def get(self, url):
"""TODO: to be defined1. """
used_proxy = random.choice(k.get().values())
used_proxy = used_proxy.data_dict['hash']
b = requests.get(url, proxies={'http': 'http://' + used_proxy})
self.write(b.text.encode('utf-8'))
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/book/([0-9]+)", BookHandler),
(r"/(http://.+)", LinkHandler)
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
k = KuaidailiProxyGenerator()
x = XicidailiProxyGenerator()
k.start()
x.start()
tornado.ioloop.IOLoop.current().start()