Skip to content

Commit 4030942

Browse files
author
huhao
committed
gevent超时后用curl做补偿
1 parent 3de0227 commit 4030942

5 files changed

+72
-34
lines changed

python/goodorbad.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
]
1414

1515
def sum1(input):
16-
return sum((lambda x: (x < 10 and [x*2] or [x/2])[0])(num)
16+
return sum((lambda x: x < 10 and x*2 or x/2)(num)
1717
for seq in inputdata
1818
if len(seq) >= 5
1919
for num in seq

python/monitor_web_all.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#encoding=utf-8
2+
'''
3+
先用gevent跑,然后把超时的用curl重试
4+
'''
15
from gevent import spawn,joinall,sleep
26
from gevent.pool import Pool
37
from monitor_web_gevent import process_results, curl as gevent_curl
@@ -52,15 +56,16 @@ def init_log(ips):
5256
with open(log_path, 'a') as f:
5357
f.write(ip)
5458

55-
def log_result(result):
56-
ip = result[1]
57-
log_path = './log/%s' % (ip,)
58-
with open(log_path, 'a') as f:
59-
try:
60-
int(result[0])
61-
f.write('*')
62-
except:
63-
f.write('#')
59+
def log_results(results):
60+
for result in results:
61+
ip = result[1]
62+
log_path = './log/%s' % (ip,)
63+
with open(log_path, 'a') as f:
64+
try:
65+
int(result[0])
66+
f.write('*')
67+
except:
68+
f.write('#')
6469

6570
def end_log(ips):
6671
for ip in ips:
@@ -72,16 +77,15 @@ def process_ips(ips):
7277
all_results = get_results_use_pool()
7378
timeout_ips = get_timeout_ips(all_results)
7479
retry_results = retry_timeout_ips(timeout_ips)
75-
merged_results = merge_results(retry_results, all_results)
76-
process_results(merged_results)
77-
for result in merged_results:
78-
log_result(result)
80+
return merge_results(retry_results, all_results)
7981

8082
if __name__ == '__main__':
8183
iplist = getiplist()
8284
init_log(iplist)
8385
for i in range(100):
84-
process_ips(iplist)
86+
results = process_ips(iplist)
87+
process_results(results)
88+
log_results(results)
8589
print 'sleep.....'
8690
sleep(30)
8791
end_log(iplist)

python/monitor_web_curl.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
CONNECT_TIMEOUT,DATA_TIMEOUT = 10, 10
1212
IP_COUNT, POOL_SIZE = 1000, 100
1313

14+
def parse_stdout(stdout):
15+
if not stdout:return None
16+
lineend_index = stdout.find('\n')
17+
if lineend_index < 0: return None
18+
firstline = stdout[0: lineend_index]
19+
match = regex_code.search(firstline)
20+
if match:
21+
return match.group(1)
22+
return None
23+
24+
def parse_stderr(stderr):
25+
if not stderr:return None
26+
match = regex_error.search(stderr)
27+
if match:
28+
return match.group(1)
29+
return None
30+
31+
32+
def parse_curl_info(stdout, stderr):
33+
info = parse_stdout(stdout)
34+
if info: return info
35+
return parse_stderr(stderr)
36+
1437
def curl(ip):
1538
'''
1639
调用curl并获取结果,从结果里提取http应答码
@@ -20,17 +43,9 @@ def curl(ip):
2043
% (CONNECT_TIMEOUT, DATA_TIMEOUT, ip)
2144
process = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
2245

23-
info = process.stdout.readline().rstrip('\r\n')
24-
info = regex_code.search(info)
25-
if info:
26-
info = info.group(1)
27-
28-
if not info:
29-
info = process.stderr.read()
30-
info = regex_error.search(info)
31-
if info:
32-
info= info.group(1)
33-
46+
stdout = process.stdout.read()
47+
stderr = process.stderr.read()
48+
info = parse_curl_info(stdout, stderr)
3449
print info, ip
3550
return info, ip
3651

python/monitor_web_gevent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
探测一组IP的可访问性
55
'''
66
from gevent import joinall,Timeout
7-
from gevent import monkey;monkey.patch_all()
7+
from gevent import monkey;monkey.patch_all(thread=False)
88
from gevent.pool import Pool
99
import urllib2
1010
import socket

python/popentimeout.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,41 @@
2222
from subprocess import Popen, PIPE
2323
from gevent.socket import wait_read
2424
from sys import exc_info
25-
from traceback import format_exception
25+
try:
26+
from errno import EBADF
27+
except ImportError:
28+
EBADF = 9
2629

2730
def runwithtimeout(cmd, timeout):
2831
process = Popen(cmd,shell=True, stdout=PIPE, stderr=PIPE,
2932
close_fds=True)
30-
f = process.stdout
33+
stdout, stderr = None, None
3134
try:
32-
wait_read(f.fileno(), timeout)
33-
return f.readlines()
34-
except:
35+
stdout = read(process.stdout, timeout)
36+
stderr = read(process.stderr, timeout)
37+
except Exception, ex:
38+
print 'popen timeout:', cmd
39+
finally:
3540
process.kill()
36-
raise
41+
return stdout, stderr
3742

43+
def read(fromfile, timeout):
44+
while True:
45+
try:
46+
wait_read(fromfile.fileno(), timeout)
47+
return fromfile.read()
48+
except:
49+
ex = exc_info()[1]
50+
if ex.args[0] == EBADF:
51+
return ''
52+
raise
53+
3854
if __name__ == '__main__':
3955
from gevent import spawn
40-
task = spawn(runwithtimeout, 'whois baidu.com', 1)
56+
from traceback import format_exception
57+
cmd = 'curl -I --connect-timeout %s -m %s %s ' \
58+
% (5, 5, '202.99.160.68')
59+
task = spawn(runwithtimeout, cmd, 10)
4160
try:
4261
print ''.join(task.get())
4362
except:

0 commit comments

Comments
 (0)