| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import requests
- from tools.log import logger
- def http_post_proxy_request(url, form_data, headers=None, proxy=None, timeout=30):
- """
- 发送带有 JSON 数据的 POST 请求,并使用代理
- 参数:
- - url: 请求的目标 URL
- - json_data: 要发送的 JSON 数据
- - headers: 可选,请求头信息
- - proxy: 可选,代理地址,例如 "http://127.0.0.1:10809"
- - timeout: 可选,请求超时时间,默认为 10 秒
- 返回值:
- - response: 请求的响应对象
- """
- try:
- # 发送 POST 请求
- proxies = {"http": proxy, "https": proxy} if proxy else None
- response = requests.post(url, data=form_data, headers=headers, proxies=proxies, timeout=timeout)
- # 检查响应状态码
- response.raise_for_status()
- # 如果状态码为 200,表示请求成功
- if response.status_code == 200:
- return response
- #
- # # 获取响应的内容类型
- # content_type = response.headers.get('Content-Type', '')
- # # 如果内容类型为 JSON,解析 JSON 数据
- # if 'application/json' in content_type:
- # json_data = response.json()
- # return json_data
- # else:
- # # 如果内容类型不是 JSON,按照其他方式处理
- # logger.error(f"http_post_request:{url}:{response}")
- # return response.text
- else:
- # 如果状态码不为 200,可以根据实际需求处理其他状态码
- print(f"Unexpected status code: {response.status_code}")
- logger.exception(f"http_post_request:{url}:{response}")
- return None
- except requests.exceptions.RequestException as e:
- # 捕获请求异常
- logger.exception(f"http_post_request:{url}:{e}:{response}")
- return None
- def http_post_request(url, data, timeout=30):
- try:
- response = requests.post(url, json=data, timeout=timeout)
- response.raise_for_status() # 检查请求是否成功
- if 200 == response.status_code:
- return response
- logger.exception(f"http_post_request - code: {response},url: {url},data: {data}")
- return None
- except requests.exceptions.RequestException as req_exc:
- print(f"HTTP POST request error: {req_exc}")
- logger.exception(f"http_post_request - Error: {req_exc}, url: {url}, data: {data}")
- return None
- except Exception as e:
- print(f"An unexpected error occurred: {e}")
- logger.exception(f"http_post_request - Unexpected error: {e}, url: {url}, data: {data}")
- return None
- def http_post_request_proxy(url, headers, data, proxy=None, timeout=30):
- try:
- proxies = {"http": proxy, "https": proxy} if proxy else None
- response = requests.post(url, headers=headers, data=data, proxies=proxies, timeout=timeout)
- response.raise_for_status() # 检查请求是否成功
- if 200 == response.status_code:
- return response
- logger.error(f"http_post_request - code: {response},url: {url},data: {data}")
- return None
- except requests.exceptions.RequestException as req_exc:
- logger.error(f"http_post_request_proxy - Error: {req_exc}, url: {url}, headers: {headers},data: {data}")
- return None
- except Exception as e:
- logger.error(f"http_post_request_proxy - Unexpected error: {e}, url: {url}, headers: {headers},data: {data}")
- return None
|