This repository has been archived on 2024-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
proxyapi/app.py

131 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
import requests
from fake_useragent import UserAgent
from flask import Flask, request, jsonify
import tools
app = Flask(__name__)
def generate_random_headers():
"""
生成随机的User-Agent头部。
:return: 随机的User-Agent头部
:rtype: dict
"""
user_agent = UserAgent().random
accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
accept_language = random.choice(
["zh-CN,zh;q=0.9",
"en-US,en;q=0.5",
"en-GB,en;q=0.5",
"de-DE,de;q=0.5",
"fr-FR,fr;q=0.5"])
accept_encoding = "gzip, deflate, br"
referer = random.choice(
["https://www.baidu.com",
"https://www.google.com",
"https://www.bing.com"])
headers = {
"User-Agent": user_agent,
"Accept": accept,
"Accept-Language": accept_language,
"Accept-Encoding": accept_encoding,
"Referer": referer,
}
return headers
@app.route('/proxy', methods=['POST'])
def proxy():
"""
代理接口,转发请求到指定的 URL。
接受 JSON 格式的请求体,其中应包含 URL、HTTP 方法和其他可选参数。
返回格式化的响应,包含状态码、消息释义和数据。
"""
req_data = request.json
# 确保提供了 url 和 method
if 'url' not in req_data or 'method' not in req_data:
return jsonify({'status': 'error', 'msg': 'URL or Method not provided', 'data': {}}), 400
url = req_data['url']
method = req_data['method'].lower()
# 支持的请求方法
supported_methods = {'get', 'post', 'put', 'delete', 'patch'}
if method not in supported_methods:
return jsonify({'status': 'error', 'msg': 'Unsupported method', 'data': {}}), 400
# 提取请求参数
params = req_data.get('params', {})
data = req_data.get('data', {})
json_data = req_data.get('json', {})
headers = req_data.get('headers', generate_random_headers())
cookies = req_data.get('cookies', {})
timeout = req_data.get('timeout', 10)
try:
# 使用 requests 发送请求
response = requests.request(method, url, params=params, data=data, json=json_data,
headers=headers, cookies=cookies, timeout=timeout)
# 尝试解析 JSON如果不是 JSON 则保持原样
try:
response_json = response.json()
except ValueError:
response_json = {}
# 构建响应数据
response_data = {
'url': response.url,
'headers': dict(response.headers),
'status_code': response.status_code,
'text': response.text,
'json': response_json,
'cookies': requests.utils.dict_from_cookiejar(response.cookies)
}
return jsonify({
'status': 'success',
'msg': 'Request successful',
'data': response_data
}), response.status_code
except requests.RequestException as e:
# 请求过程中出现异常
return jsonify({
'status': 'error',
'msg': str(e),
'data': {}
}), 500
@app.route('/download', methods=['GET'])
def download():
url = request.args.get('url')
if not url:
return jsonify({
'status': 'error',
'msg': 'No URL provided',
'data': {}
}), 400
content = tools.download_html_with_wget(url)
if content is None:
return jsonify({
'status': 'error',
'msg': 'Unable to download the content',
'data': {}
}), 500
return content
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)