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