上节课我们略微重构了一下这个wqrf_run_case.py。
本节课继续开发,开始要发送请求了:
而请求要分为以下几个情况
(注意,其中实现代码均为postman生成源码参考并增加了灵活度,且经过了漫长的千余位同行的使用和数次纠正及优化,若仍有bug,请及时反馈,感谢贡献。)
可复制版本:
'执行请求' ## none请求 if api_body_method == 'none' or api_body_method == 'null': if type(login_res) == dict: response = requests.request(api_method.upper(), url, headers=header, data={}, cert=cert_name) else: response = login_res.request(api_method.upper(), url, headers=header, data={}, cert=cert_name) ## form-data请求 elif api_body_method == 'form-data': files = [] payload = () for i in eval(api_body): payload += ((i[0], i[1]),) if type(login_res) == dict: for i in login_res.keys(): payload += ((i, login_res[i]),) response = requests.request(api_method.upper(), url, headers=header, data=payload, files=files, cert=cert_name) else: response = login_res.request(api_method.upper(), url, headers=header, data=payload, files=files, cert=cert_name) ## x-www-form-urlencoded请求 elif api_body_method == 'x-www-form-urlencoded': header['Content-Type'] = 'application/x-www-form-urlencoded' payload = () for i in eval(api_body): payload += ((i[0], i[1]),) if type(login_res) == dict: for i in login_res.keys(): payload += ((i, login_res[i]),) if type(login_res) == dict: response = requests.request(api_method.upper(), url, headers=header, data=payload, cert=cert_name) else: response = login_res.request(api_method.upper(), url, headers=header, data=payload, cert=cert_name) ## GraphQL请求 elif api_body_method == 'GraphQL': header['Content-Type'] = 'application/json' query = api_body.split('*WQRF*')[0] graphql = api_body.split('*WQRF*')[1] try: eval(graphql) except: graphql = '{}' payload = '{"query":"%s","variables":%s}' % (query, graphql) if type(login_res) == dict: response = requests.request(api_method.upper(), url, headers=header, data=payload, cert=cert_name) else: response = login_res.request(api_method.upper(), url, headers=header, data=payload, cert=cert_name) ## raw的五种请求 else: # 这时肯定是raw的五个子选项: if api_body_method == 'Text': header['Content-Type'] = 'text/plain' if api_body_method == 'JavaScript': header['Content-Type'] = 'text/plain' if api_body_method == 'Json': api_body = json.loads(api_body) for i in login_res.keys(): api_body[i] = login_res[i] api_body = json.dumps(api_body) header['Content-Type'] = 'text/plain' if api_body_method == 'Html': header['Content-Type'] = 'text/plain' if api_body_method == 'Xml': header['Content-Type'] = 'text/plain' if type(login_res) == dict: response = requests.request(api_method.upper(), url, headers=header, data=api_body.encode('utf-8'), cert=cert_name) else: response = login_res.request(api_method.upper(), url, headers=header, data=api_body.encode('utf-8'), cert=cert_name) response.encoding = "utf-8" res = response.text #最终结果文案
注意,还有以下可选:
这里奉上另一种简易化的发送代码,大家也可以替换这个:
data = {} if data["method"] == "GET": response = requests.get( data["url"], params=data["body"], headers=data["header"], cookies=data["cookies"], proxies=data["proxy"]) elif data["header"]["Content-Type"] == "application/json": response = requests.post( data["url"], json=data["body"], headers=data["header"], cookies=data["cookies"], proxies=data["proxy"]) else : response = requests.post( data["url"], data=data["body"], headers=data["header"], cookies=data["cookies"], proxies=data["proxy"])
然后是 具体去哪里矫正和参考:
好了,本期内容到此结束,欢迎继续追更。