Skip to content

Commit b9b84ac

Browse files
author
caofan
committed
cookie is headache
1 parent 2e2b3a6 commit b9b84ac

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

auto/batchHandler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
no_ca = "--verify=no"
2020
httpie_allow_view = {"-v": "显示请求详细信息", "-h": "显示请求头", "-b": "显示请求Body", "-d": "响应结果保存至TXT", "": "默认"}
2121
httpie_view = None
22-
# 并发数
22+
# 最大并发数
2323
max_concurrent = 64
2424
concurrent = 1
2525
try:
@@ -130,7 +130,9 @@ def auto_login():
130130
# request_headers = {"Content-Type": "application/json", "HT-app": "6"}
131131
response = requests.request(method, url, headers=request_headers, json=request_body, timeout=3, verify=False)
132132
response_headers = response.headers
133-
cookie = response_headers.get("set-Cookie")
133+
# 处理Cookie, 多个Cookie之间使用';'分隔, 否则校验cookie时出现"domain."在高版本中tomcat中报错
134+
# https://blog.csdn.net/w57685321/article/details/84943176
135+
cookie = response_headers.get("set-Cookie").replace(", _r", "; _r").replace(", _a", "; _a")
134136
# JSON标准格式
135137
response_body = json.dumps(response.json(), ensure_ascii=False, indent=4)
136138
print("登录响应Cookie结果: \n{}\n登录响应BODY结果: {}".format(cookie, response_body))

concurrent_test/concurrent_test.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,31 @@
1111
# 屏蔽HTTPS证书校验, 忽略安全警告
1212
requests.packages.urllib3.disable_warnings()
1313
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
2022
if len(sys.argv) > 1:
2123
try:
22-
input_concurrent = int(sys.argv[2])
24+
input_concurrent = int(sys.argv[1])
2325
if input_concurrent > 1:
2426
concurrent = min(input_concurrent, max_concurrent)
2527
except Exception as e:
2628
print("并发数设置范围[1, {}], 默认1".format(max_concurrent))
2729
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]
3139

3240

3341
def execute_http(i):
@@ -72,7 +80,9 @@ def auto_login():
7280
# request_headers = {"Content-Type": "application/json", "HT-app": "6"}
7381
response = requests.request(method, url, headers=request_headers, json=request_body, timeout=3, verify=False)
7482
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")
7686
# JSON标准格式
7787
response_body = json.dumps(response.json(), ensure_ascii=False, indent=4)
7888
print("登录响应Cookie结果: \n{}\n登录响应BODY结果: {}".format(cookie, response_body))
@@ -91,13 +101,20 @@ def handle_json_str_value(json):
91101

92102

93103
def main():
94-
# 全局变量cookie, 初始化为空
104+
# 初始化参数
105+
initial_param_list = init_param()
106+
# 全局变量
107+
global joiner
108+
global execute_num
95109
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)
101118

102119

103120
if __name__ == '__main__':

0 commit comments

Comments
 (0)