MCPcopy Index your code
hub / github.com/smallfawn/QLScriptPublic / request

Method request

daily/sfsy.py:290–381  ·  view source on GitHub ↗

发送HTTP请求,带双层重试机制 Args: url: 请求URL method: 请求方法 GET/POST data: 请求数据 max_retries: 最大重试次数 Returns: 响应JSON数据或None

(
        self, 
        url: str, 
        method: str = 'POST', 
        data: Optional[Dict] = None,
        max_retries: int = REQUEST_RETRY_COUNT
    )

Source from the content-addressed store, hash-verified

288 }
289
290 def request(
291 self,
292 url: str,
293 method: str = 'POST',
294 data: Optional[Dict] = None,
295 max_retries: int = REQUEST_RETRY_COUNT
296 ) -> Optional[Dict[str, Any]]:
297 """发送HTTP请求,带双层重试机制
298
299 Args:
300 url: 请求URL
301 method: 请求方法 GET/POST
302 data: 请求数据
303 max_retries: 最大重试次数
304
305 Returns:
306 响应JSON数据或None
307 """
308 # 更新签名
309 sign_data = self._generate_sign()
310 self.headers.update(sign_data)
311
312 retry_count = 0
313 proxy_retry_count = 0
314
315 while proxy_retry_count < MAX_PROXY_RETRIES:
316 try:
317 # 如果请求重试次数达到2次,尝试切换代理
318 if retry_count >= 2:
319 print('请求已失败2次,尝试切换代理IP')
320 new_proxy = self.proxy_manager.get_proxy()
321 if new_proxy:
322 self.session.proxies = new_proxy
323 else:
324 print('⚠️ 切换代理失败,无可用代理')
325 retry_count = 0 # 重置请求重试计数
326
327 try:
328 if method.upper() == 'GET':
329 response = self.session.get(url, headers=self.headers, timeout=PROXY_TIMEOUT)
330 elif method.upper() == 'POST':
331 response = self.session.post(url, headers=self.headers, json=data or {}, timeout=PROXY_TIMEOUT)
332 else:
333 raise ValueError(f'不支持的请求方法: {method}')
334
335 # 检查响应状态码
336 response.raise_for_status()
337
338 try:
339 res = response.json()
340 if res is None:
341 print(f'响应内容为空,正在重试 ({retry_count + 1}/{max_retries})')
342 retry_count += 1
343 time.sleep(2)
344 continue
345 return res
346 except (json.JSONDecodeError, ValueError) as e:
347 print(f'JSON解析失败: {str(e)}, 响应内容: {response.text[:200]}')

Callers 8

app_sign_inMethod · 0.45
sign_inMethod · 0.45
new_sign_inMethod · 0.45
get_task_listMethod · 0.45
execute_taskMethod · 0.45
receive_task_rewardMethod · 0.45
get_welfare_listMethod · 0.45
receive_welfareMethod · 0.45

Calls 6

_generate_signMethod · 0.95
get_proxyMethod · 0.80
sleepMethod · 0.80
printFunction · 0.70
getMethod · 0.45
postMethod · 0.45

Tested by

no test coverage detected