sfun commited on
Commit
27ff0e4
1 Parent(s): a1b4ebb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -110,20 +110,20 @@ def get_client_ip(request):
110
 
111
  async def handle_request(request):
112
  if request.path == '/':
113
- query_params = parse_qs(request.query_string)
114
- if 'url' in query_params:
115
- url = query_params['url'][0]
116
- no_cache = 'nocache' in query_params
117
  cache_entry = None if no_cache else cache.get(url)
118
  cache_hit = False
119
  new_data = False
120
 
121
- if cache_entry:
122
  result = cache_entry.data
123
  cache_hit = True
124
  cache_time = cache_entry.timestamp
125
 
126
- if not cache_hit or no_cache or (datetime.datetime.now() - cache_entry.timestamp).total_seconds() >= 1800:
127
  try:
128
  async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:
129
  input_text = await fetch_url(url, session)
@@ -175,14 +175,14 @@ async def logging_middleware(request, handler):
175
  timestamp = end_time.strftime('%Y-%m-%d %H:%M:%S')
176
  client_ip = get_client_ip(request)
177
  target_url = request.query.get('url', '-')
 
178
  status_code = response.status
179
  proxy_count = response.headers.get('X-Proxy-Count', '0')
180
  cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
181
  cache_time = response.headers.get('X-Cache-Time', '-')
182
  new_data = "Yes" if response.headers.get('X-New-Data') == 'True' else "No"
183
- no_cache = "Yes" if response.headers.get('X-No-Cache') == 'True' else "No"
184
 
185
- log_message = f"{timestamp} - {client_ip} - \"GET /?url={target_url}\" - Status: {status_code} - Proxies: {proxy_count} - Cache: {cache_hit} - CacheTime: {cache_time} - NewData: {new_data} - NoCache: {no_cache}"
186
  print(log_message, flush=True)
187
 
188
  return response
 
110
 
111
  async def handle_request(request):
112
  if request.path == '/':
113
+ # 使用 request.query 替代 parse_qs
114
+ if 'url' in request.query:
115
+ url = request.query['url']
116
+ no_cache = 'nocache' in request.query
117
  cache_entry = None if no_cache else cache.get(url)
118
  cache_hit = False
119
  new_data = False
120
 
121
+ if cache_entry and not no_cache:
122
  result = cache_entry.data
123
  cache_hit = True
124
  cache_time = cache_entry.timestamp
125
 
126
+ if not cache_hit or no_cache:
127
  try:
128
  async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:
129
  input_text = await fetch_url(url, session)
 
175
  timestamp = end_time.strftime('%Y-%m-%d %H:%M:%S')
176
  client_ip = get_client_ip(request)
177
  target_url = request.query.get('url', '-')
178
+ no_cache = 'nocache' in request.query
179
  status_code = response.status
180
  proxy_count = response.headers.get('X-Proxy-Count', '0')
181
  cache_hit = "Hit" if response.headers.get('X-Cache-Hit') == 'True' else "Miss"
182
  cache_time = response.headers.get('X-Cache-Time', '-')
183
  new_data = "Yes" if response.headers.get('X-New-Data') == 'True' else "No"
 
184
 
185
+ log_message = f"{timestamp} - {client_ip} - \"GET /?url={target_url}{'&nocache' if no_cache else ''}\" - Status: {status_code} - Proxies: {proxy_count} - Cache: {cache_hit} - CacheTime: {cache_time} - NewData: {new_data} - NoCache: {'Yes' if no_cache else 'No'}"
186
  print(log_message, flush=True)
187
 
188
  return response