hikyuu2/hikyuu/fetcher/proxy/proxy.py

36 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding:utf-8
#
# The MIT License (MIT)
#
# Created on: 2020-11-29
# Author: fasiondog
import requests
import json
import datetime
from hikyuu.util import hku_warn, hku_info, hku_check
from .zhima import get_proxy
def request_with_proxy(url):
"""通过代理进行请求,访问失败将抛出异常"""
# 获取到的 ip 可能无法访问相应的 url重试10次以便找到能用的 proxy
new = False
proxies = {'http': '127.0.0.1'}
for i in range(10): # pylint: disable=unused-variable
try:
proxy = get_proxy(new)
hku_check(proxy, "Failed get proxy!")
proxies = {'http': 'http://{}'.format(proxy)}
result = requests.get(url, proxies=proxies).text
#hku_info("use proxy: {}".format(proxies['http']))
return result
except:
new = True
raise Exception("无法通过代理访问!")
def request_with_local(url):
"""通过本机ip直接获取请求访问失败将抛出异常"""
return requests.get(url).text