4
4
import json
5
5
import ssl
6
6
import unicodedata
7
- import sys
8
- import time
9
7
from concurrent .futures .thread import ThreadPoolExecutor
10
8
11
9
import pandas as pd
10
+ import numpy as np
12
11
import requests
12
+ import argparse
13
13
14
14
# 屏蔽HTTPS证书校验, 忽略安全警告
15
15
requests .packages .urllib3 .disable_warnings ()
16
16
context = ssl ._create_unverified_context ()
17
- joiner = ' '
18
- cmd = "http"
19
- no_ca = "--verify=no"
20
- httpie_allow_view = {"-v" : "显示请求详细信息" , "-h" : "显示请求头" , "-b" : "显示请求Body" , "-d" : "响应结果保存至TXT" , "" : "默认" }
21
- httpie_view = None
22
- # 最大并发数
23
- max_concurrent = 64
24
- concurrent = 1
25
- try :
26
- if len (sys .argv ) > 1 :
27
- if httpie_allow_view .get (sys .argv [1 ]) is not None :
28
- httpie_view = sys .argv [1 ]
29
- else :
30
- print ("输入参数有误, 仅支持如下参数: -v显示请求详细信息|-h显示请求头|-b显示请求Body|-d响应结果保存至TXT" )
31
- if len (sys .argv ) > 2 :
32
- try :
33
- input_concurrent = int (sys .argv [2 ])
34
- if input_concurrent > 1 :
35
- concurrent = min (input_concurrent , max_concurrent )
36
- except Exception as e :
37
- print ("并发数设置范围[1, {}], 默认1" .format (max_concurrent ))
38
- print (e )
39
- except Exception as e :
40
- print (e )
41
- executor = ThreadPoolExecutor (max_workers = concurrent )
42
-
43
-
44
- def httpie_cmd (id ):
45
- """
46
- 执行excuteUrl.json接口
47
- :param id
48
- :return:
17
+
18
+
19
+ def init_param () -> list :
20
+ """
21
+ 初始化参数, 读取shell命令参数, 自动登录
22
+ 依次返回httpie_view方式, 线程池, 登录cookie
23
+ :rtype: list
24
+ """
25
+ parser = argparse .ArgumentParser (description = "并发执行接口" )
26
+ parser .add_argument ("-w" , "--workers" , type = int , choices = choice_nums (1 , 65 , 1 ), default = 1 , help = "并发执行线程数, 取值范围[1, 64]" )
27
+ group = parser .add_mutually_exclusive_group ()
28
+ group .add_argument ("-v" , "--view" , action = "store_true" , help = "显示请求详细信息" )
29
+ group .add_argument ("-hd" , "--header" , action = "store_true" , help = "显示请求头" )
30
+ group .add_argument ("-b" , "--body" , action = "store_true" , help = "显示请求Body" )
31
+ group .add_argument ("-d" , "--download" , action = "store_true" , help = "显示请求头, 但响应结果保存至TXT" )
32
+ args = parser .parse_args ()
33
+ view_param = "-v"
34
+ if args .header :
35
+ view_param = "-h"
36
+ if args .body :
37
+ view_param = "-b"
38
+ if args .download :
39
+ view_param = "-d"
40
+ print ("参数设置结果: httpie命令方式=[{}], 并发线程数=[{}]" .format (view_param , args .workers ))
41
+ init_executor = ThreadPoolExecutor (max_workers = args .workers )
42
+ cookie = auto_login ()
43
+ return [view_param , init_executor , cookie ]
44
+
45
+
46
+ def execute_http (id : int ) -> str :
47
+ """
48
+ 执行excuteUrl.json接口, 返回结果数据
49
+ :param id: 接口请求标识性ID数据
50
+ :rtype: str
49
51
"""
50
52
with open ("./excuteUrl.json" , 'r' ) as request_data :
51
53
request_json = json .load (request_data )
@@ -71,10 +73,19 @@ def httpie_cmd(id):
71
73
return "执行命令httpie:\n {}\n 当前ID=[{}], executeStartTime=[{}], executeEndTime=[{}]\n 响应结果:\n {}" .format (httpie_cmd_str , id , executeStartTime , executeEndTime , response_body )
72
74
73
75
74
- def httpie (url , method , request_headers , request_body ):
75
- param_list = [cmd , no_ca ]
76
- if httpie_view is not None :
77
- param_list .append (httpie_view )
76
+ def httpie (url : str , method : str , request_headers : json , request_body : json ) -> str :
77
+ """
78
+ 拼接httpie完整命令
79
+ :param url: 接口访问路径
80
+ :param method: 请求方式
81
+ :param request_headers: 请求头JSON
82
+ :param request_body: 请求Body体JSON
83
+ :rtype: str
84
+ """
85
+ joiner = ' '
86
+ cmd = "http"
87
+ no_ca = "--verify=no"
88
+ param_list = [cmd , no_ca , httpie_view ]
78
89
param_list .extend ([method , url ])
79
90
for (k , v ) in request_headers .items ():
80
91
if k == "Cookie" :
@@ -89,9 +100,12 @@ def httpie(url, method, request_headers, request_body):
89
100
return joiner .join (param_list )
90
101
91
102
92
- def is_number (s ):
103
+ def is_number (s : str ) -> bool :
104
+ """
105
+ :param s: 输入字符串
106
+ :rtype: bool
107
+ """
93
108
try :
94
- # 如果能运行float(s)语句,返回True(字符串s是浮点数)
95
109
float (s )
96
110
return True
97
111
except ValueError :
@@ -107,19 +121,20 @@ def is_number(s):
107
121
return False
108
122
109
123
110
- def load_data ():
124
+ def load_data () -> list :
111
125
"""
112
126
读取数据文件, 每行为一条数据
113
- :return:
127
+ :rtype: list
114
128
"""
115
129
data = pd .read_csv ("./ID.csv" , header = - 1 )
116
130
data .columns = ['id' ]
117
131
return data ['id' ]
118
132
119
133
120
- def auto_login ():
134
+ def auto_login () -> str :
121
135
"""
122
136
自动登录, 获取登录Cookie
137
+ :rtype: str
123
138
"""
124
139
with open ("./ssoLogin.json" , 'r' ) as sso_login_request_data :
125
140
request_json = json .load (sso_login_request_data )
@@ -139,22 +154,23 @@ def auto_login():
139
154
return cookie
140
155
141
156
142
- def handle_json_str_value (json ) :
157
+ def handle_json_str_value (json : json ) -> json :
143
158
"""
144
159
将json的值都变为字符串处理
145
160
:param json:
146
- :return:
161
+ :rtype: json
147
162
"""
148
163
for (k , v ) in json .items ():
149
164
json [k ] = str (v )
150
165
return json
151
166
152
167
153
- def replace_id (json , id ) :
168
+ def replace_id (json : json , id : int ) -> json :
154
169
"""
155
170
将json的值都变为字符串处理
156
171
:param json:
157
- :return:
172
+ :param id: 目标ID
173
+ :rtype: json
158
174
"""
159
175
for (k , v ) in json .items ():
160
176
if v == "NONE" :
@@ -164,14 +180,27 @@ def replace_id(json, id):
164
180
return json
165
181
166
182
183
+ def choice_nums (start : int , end : int , delta : int ) -> list :
184
+ """
185
+ 返回指定的数组序列
186
+ :rtype: list
187
+ """
188
+ return np .arange (start , end , delta ).tolist ()
189
+
190
+
167
191
def main ():
168
- # 全局变量cookie, 初始化为空
192
+ # 全局变量
193
+ global httpie_view
194
+ global executor
169
195
global init_cookie
170
- # 首先登陆一次
171
- init_cookie = auto_login ()
196
+ # 首先初始化数据
197
+ init = init_param ()
198
+ httpie_view = init [0 ]
199
+ executor = init [1 ]
200
+ init_cookie = init [2 ]
172
201
# 读取ID数据列表
173
202
ids = load_data ()
174
- for result in executor .map (httpie_cmd , ids ):
203
+ for result in executor .map (execute_http , ids ):
175
204
print (result )
176
205
177
206
0 commit comments