From f34a5c28290d0ac10ceab79253245321dfdfabea Mon Sep 17 00:00:00 2001 From: zhouzhongping Date: Mon, 15 Jan 2024 13:22:54 +0800 Subject: [PATCH] project: update random headers --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- app.py | 41 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Pipfile b/Pipfile index 10f9dc3..716a7a2 100644 --- a/Pipfile +++ b/Pipfile @@ -6,6 +6,7 @@ name = "pypi" [packages] flask = "*" requests = "*" +fake-useragent = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 17d8499..2627da3 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "84eecd763fb3f9782dcf46fa649cfb695d35cee52e6e15c328de4cdf284ca2c0" + "sha256": "5a231433240cdafac16c5a16c67ef14683b60219c02f92556fd9d8a52f6063e7" }, "pipfile-spec": 6, "requires": { @@ -137,6 +137,14 @@ "markers": "python_version >= '3.7'", "version": "==8.1.7" }, + "fake-useragent": { + "hashes": [ + "sha256:5426e4015d8ccc5bb25f64d3dfcfd3915eba30ffebd31b86b60dc7a4c5d65528", + "sha256:9acce439ee2c6cf9c3772fa6c200f62dc8d56605063327a4d8c5d0e47f414b85" + ], + "index": "pypi", + "version": "==1.4.0" + }, "flask": { "hashes": [ "sha256:21128f47e4e3b9d597a3e8521a329bf56909b690fcc3fa3e477725aa81367638", diff --git a/app.py b/app.py index 60d2396..3d816e1 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,44 @@ +import random + import requests +from fake_useragent import UserAgent from flask import Flask, request, jsonify 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(): """ @@ -29,16 +64,14 @@ def proxy(): params = req_data.get('params', {}) data = req_data.get('data', {}) json_data = req_data.get('json', {}) - headers = req_data.get('headers', {}) + headers = req_data.get('headers', generate_random_headers()) cookies = req_data.get('cookies', {}) timeout = req_data.get('timeout', 10) - allow_redirects = req_data.get('allow_redirects', True) try: # 使用 requests 发送请求 response = requests.request(method, url, params=params, data=data, json=json_data, - headers=headers, cookies=cookies, timeout=timeout, - allow_redirects=allow_redirects) + headers=headers, cookies=cookies, timeout=timeout) # 尝试解析 JSON,如果不是 JSON 则保持原样 try: