11
11
# 屏蔽HTTPS证书校验, 忽略安全警告
12
12
requests .packages .urllib3 .disable_warnings ()
13
13
context = ssl ._create_unverified_context ()
14
- joiner = ' '
15
- # 并发数
16
- max_concurrent = 64
17
- concurrent = 1
18
- # 是否开启并发测试
19
- try :
14
+
15
+
16
+ def init_param ():
17
+ joiner = ' '
18
+ # 并发数
19
+ max_concurrent = 64
20
+ concurrent = 1
21
+ execute_num = 1
20
22
if len (sys .argv ) > 1 :
21
23
try :
22
- input_concurrent = int (sys .argv [2 ])
24
+ input_concurrent = int (sys .argv [1 ])
23
25
if input_concurrent > 1 :
24
26
concurrent = min (input_concurrent , max_concurrent )
25
27
except Exception as e :
26
28
print ("并发数设置范围[1, {}], 默认1" .format (max_concurrent ))
27
29
print (e )
28
- except Exception as e :
29
- print (e )
30
- executor = ThreadPoolExecutor (max_workers = concurrent )
30
+ if len (sys .argv ) > 2 :
31
+ try :
32
+ if int (sys .argv [2 ]) > 1 :
33
+ execute_num = int (sys .argv [2 ])
34
+ except Exception as e :
35
+ print (e )
36
+ init_cookie = auto_login ()
37
+ executor = ThreadPoolExecutor (max_workers = concurrent )
38
+ return [joiner , execute_num , init_cookie , executor ]
31
39
32
40
33
41
def execute_http (i ):
@@ -72,7 +80,9 @@ def auto_login():
72
80
# request_headers = {"Content-Type": "application/json", "HT-app": "6"}
73
81
response = requests .request (method , url , headers = request_headers , json = request_body , timeout = 3 , verify = False )
74
82
response_headers = response .headers
75
- cookie = response_headers .get ("set-Cookie" )
83
+ # 处理Cookie, 多个Cookie之间使用';'分隔, 否则校验cookie时出现"domain."在高版本中tomcat中报错
84
+ # https://blog.csdn.net/w57685321/article/details/84943176
85
+ cookie = response_headers .get ("set-Cookie" ).replace (", _r" , "; _r" ).replace (", _a" , "; _a" )
76
86
# JSON标准格式
77
87
response_body = json .dumps (response .json (), ensure_ascii = False , indent = 4 )
78
88
print ("登录响应Cookie结果: \n {}\n 登录响应BODY结果: {}" .format (cookie , response_body ))
@@ -91,13 +101,20 @@ def handle_json_str_value(json):
91
101
92
102
93
103
def main ():
94
- # 全局变量cookie, 初始化为空
104
+ # 初始化参数
105
+ initial_param_list = init_param ()
106
+ # 全局变量
107
+ global joiner
108
+ global execute_num
95
109
global init_cookie
96
- init_cookie = auto_login ()
97
- nums = list (range (1 , 20 ))
98
- while True :
99
- for result in executor .map (execute_http , nums ):
100
- print (result )
110
+ global executor
111
+ joiner = initial_param_list [0 ]
112
+ execute_num = initial_param_list [1 ]
113
+ init_cookie = initial_param_list [2 ]
114
+ executor = initial_param_list [3 ]
115
+ nums = list (range (0 , execute_num ))
116
+ for result in executor .map (execute_http , nums ):
117
+ print (result )
101
118
102
119
103
120
if __name__ == '__main__' :
0 commit comments