zhepama commited on
Commit
c5cc040
·
verified ·
1 Parent(s): c61fa58

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -27
main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import cloudscraper
2
  from fastapi import FastAPI, HTTPException, Request, Response
3
  from fastapi.middleware.cors import CORSMiddleware
@@ -48,37 +49,25 @@ async def proxy(request: Request, path: str, target_url: Optional[str] = None):
48
  # 移除可能导致问题的头
49
  headers.pop("host", None)
50
  headers.pop("content-length", None)
51
- headers.update({
52
- "sec-fetch-dest": "document",
53
- })
54
  # 检查是否请求流式响应
55
  stream_request = "stream" in request.query_params and request.query_params["stream"].lower() in ["true", "1", "yes"]
56
 
 
57
  # 创建cloudscraper实例
58
- scraper = cloudscraper.create_scraper(
59
- browser={
60
- 'browser': 'chrome',
61
- 'platform': 'windows',
62
- 'desktop': True
63
- },
64
- delay=10
65
- )
66
- grok_response = scraper.get("https://grok.com/",headers={
67
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
68
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
69
- 'Accept-Language': 'en-US,en;q=0.5',
70
- 'Accept-Encoding': 'gzip, deflate, br',
71
- 'Referer': 'https://grok.com/', # 添加一个合理的来源
72
- 'DNT': '1',
73
- 'Connection': 'keep-alive',
74
- 'Upgrade-Insecure-Requests': '1',
75
- 'Sec-Fetch-Dest': 'document',
76
- 'Sec-Fetch-Mode': 'navigate',
77
- 'Sec-Fetch-Site': 'cross-site',
78
- 'Sec-Fetch-User': '?1',
79
- 'Cache-Control': 'max-age=0',
80
- })
81
- print(f"grok主页状态码: {grok_response.status_code}")
82
  # 获取请求体
83
  body = await request.body()
84
 
 
1
+ import os
2
  import cloudscraper
3
  from fastapi import FastAPI, HTTPException, Request, Response
4
  from fastapi.middleware.cors import CORSMiddleware
 
49
  # 移除可能导致问题的头
50
  headers.pop("host", None)
51
  headers.pop("content-length", None)
52
+
 
 
53
  # 检查是否请求流式响应
54
  stream_request = "stream" in request.query_params and request.query_params["stream"].lower() in ["true", "1", "yes"]
55
 
56
+ # 创建带有代理的 scraper
57
  # 创建cloudscraper实例
58
+ scraper = cloudscraper.create_scraper()
59
+ # 检查环境变量PROXY是否存在
60
+ proxy = os.environ.get('PROXY')
61
+ if proxy:
62
+ # 如果环境变量存在,则设置代理
63
+ scraper.proxies = {
64
+ 'http': proxy,
65
+ 'https': proxy
66
+ }
67
+ # 测试代理是否生效
68
+ response = scraper.get('https://httpbin.org/ip')
69
+ print(response.text)
70
+
 
 
 
 
 
 
 
 
 
 
 
71
  # 获取请求体
72
  body = await request.body()
73