project: update random headers

This commit is contained in:
周中平 2024-01-15 13:22:54 +08:00
parent 1c2878035e
commit f34a5c2829
Signed by: zhouzhongping
GPG Key ID: 6666822800008000
3 changed files with 47 additions and 5 deletions

View File

@ -6,6 +6,7 @@ name = "pypi"
[packages] [packages]
flask = "*" flask = "*"
requests = "*" requests = "*"
fake-useragent = "*"
[dev-packages] [dev-packages]

10
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "84eecd763fb3f9782dcf46fa649cfb695d35cee52e6e15c328de4cdf284ca2c0" "sha256": "5a231433240cdafac16c5a16c67ef14683b60219c02f92556fd9d8a52f6063e7"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -137,6 +137,14 @@
"markers": "python_version >= '3.7'", "markers": "python_version >= '3.7'",
"version": "==8.1.7" "version": "==8.1.7"
}, },
"fake-useragent": {
"hashes": [
"sha256:5426e4015d8ccc5bb25f64d3dfcfd3915eba30ffebd31b86b60dc7a4c5d65528",
"sha256:9acce439ee2c6cf9c3772fa6c200f62dc8d56605063327a4d8c5d0e47f414b85"
],
"index": "pypi",
"version": "==1.4.0"
},
"flask": { "flask": {
"hashes": [ "hashes": [
"sha256:21128f47e4e3b9d597a3e8521a329bf56909b690fcc3fa3e477725aa81367638", "sha256:21128f47e4e3b9d597a3e8521a329bf56909b690fcc3fa3e477725aa81367638",

41
app.py
View File

@ -1,9 +1,44 @@
import random
import requests import requests
from fake_useragent import UserAgent
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
app = Flask(__name__) 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']) @app.route('/proxy', methods=['POST'])
def proxy(): def proxy():
""" """
@ -29,16 +64,14 @@ def proxy():
params = req_data.get('params', {}) params = req_data.get('params', {})
data = req_data.get('data', {}) data = req_data.get('data', {})
json_data = req_data.get('json', {}) json_data = req_data.get('json', {})
headers = req_data.get('headers', {}) headers = req_data.get('headers', generate_random_headers())
cookies = req_data.get('cookies', {}) cookies = req_data.get('cookies', {})
timeout = req_data.get('timeout', 10) timeout = req_data.get('timeout', 10)
allow_redirects = req_data.get('allow_redirects', True)
try: try:
# 使用 requests 发送请求 # 使用 requests 发送请求
response = requests.request(method, url, params=params, data=data, json=json_data, response = requests.request(method, url, params=params, data=data, json=json_data,
headers=headers, cookies=cookies, timeout=timeout, headers=headers, cookies=cookies, timeout=timeout)
allow_redirects=allow_redirects)
# 尝试解析 JSON如果不是 JSON 则保持原样 # 尝试解析 JSON如果不是 JSON 则保持原样
try: try: