11
11
CONNECT_TIMEOUT ,DATA_TIMEOUT = 10 , 10
12
12
IP_COUNT , POOL_SIZE = 1000 , 100
13
13
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
+
14
37
def curl (ip ):
15
38
'''
16
39
调用curl并获取结果,从结果里提取http应答码
@@ -20,17 +43,9 @@ def curl(ip):
20
43
% (CONNECT_TIMEOUT , DATA_TIMEOUT , ip )
21
44
process = Popen (cmd , shell = True , stdout = PIPE , stderr = PIPE )
22
45
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 )
34
49
print info , ip
35
50
return info , ip
36
51
0 commit comments