做自动化测试的时候Postman判断数据什么的太麻烦,做个脚本代替一下
curl_parser.py
import redef parse_curl(curl_command):# 匹配所有的 -H "key: value"header_pattern = re.compile(r'-H "([^:]+): ([^"]+)"')headers = dict(header_pattern.findall(curl_command))# 匹配URLurl_pattern = re.compile(r'(https?://[^\s]+)')url_match = url_pattern.search(curl_command)url = url_match.group(0) if url_match else Nonereturn url, headers
main.py
import requests
import json
from curl_parser import parse_curldef make_request(url, headers):payload = {}try:response = requests.post(url, headers=headers, data=json.dumps(payload))if response.status_code == 200:return response.json() # 假设返回值是JSONelse:print(f"Request failed with status code: {response.status_code}")return Noneexcept requests.RequestException as e:print(f"Request failed: {e}")return Nonedef main():curl_command = '''curl复制过来'''url, headers = parse_curl(curl_command)response_data = make_request(url, headers)
#在这里就可以使用返回数据了if __name__ == "__main__":main()