forked from Earthson/spider_tornado
/
httper.py
70 lines (63 loc) · 2.37 KB
/
httper.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
#!/usr/bin/env python
import types
import sys
import urllib.parse
import random
from webtools import get_charset
from traceback import format_exc
import tornado
from tornado import httpclient, iostream, gen, ioloop
from redis_queue import SpiderQueue
io_loop = ioloop.IOLoop.instance()
httpcli = httpclient.AsyncHTTPClient()
@gen.coroutine
def queue_recover(spider_q):
yield from spider_q.recover()
@gen.coroutine
def httptasker(spider_q, retry=5):
while True:
req, callback = yield from spider_q.get(interval=1.0)
if req is None:
tl = random.random()*0.001
yield gen.Task(io_loop.add_timeout, tl)
continue
def get_response():
for i in range(retry):
try:
rq = httpclient.HTTPRequest(**req)
response = yield httpcli.fetch(rq)
response.ourl = req['url']
return response
except httpclient.HTTPError as e:
print('@HTTPError: %s with URL: %s Retry: %s' % (e.code, req['url'], i+1), file=sys.stderr)
if e.code in (403, 404):
break
continue
except Exception as e:
print('@ErrorProcess: %s\nURL: %s' % (e, req['url']), file=sys.stderr)
print('@error_trace_back', format_exc(), file=sys.stderr)
break
return None
response = yield from get_response()
try:
if response is None:
yield from spider_q.fail(req['url'])
print('@Failed: %s' % req['url'], file=sys.stderr)
continue
else:
yield from spider_q.ack(req['url'])
c = get_charset(response, default='gb18030')
response.ubody = response.body.decode(c, 'ignore')
response.charset = c
g = callback(response)
if isinstance(g, types.GeneratorType):
yield from g
except Exception as e:
print('@ErrorProcess: %s\nURL: %s' % (e, req['url']), file=sys.stderr)
print('@error_trace_back', format_exc(), file=sys.stderr)
def asyncdo(spider_q, n=3):
#queue_recover(spider_q)
tasks = [httptasker(spider_q, 8) for i in range(n)]
for t in tasks:
io_loop.run_sync(lambda:t)
#io_loop.close()