2020-12-05 17:53:38 +08:00
|
|
|
|
# coding:utf-8
|
|
|
|
|
#
|
|
|
|
|
# The MIT License (MIT)
|
|
|
|
|
#
|
|
|
|
|
# Created on: 2020-11-29
|
|
|
|
|
# Author: fasiondog
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
import json
|
|
|
|
|
import datetime
|
2020-12-07 00:27:55 +08:00
|
|
|
|
from hikyuu.util import hku_warn, hku_info, hku_check
|
2020-12-05 17:53:38 +08:00
|
|
|
|
|
|
|
|
|
from .zhima import get_proxy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def request_with_proxy(url):
|
|
|
|
|
"""通过代理进行请求,访问失败将抛出异常"""
|
|
|
|
|
# 获取到的 ip 可能无法访问相应的 url,重试10次,以便找到能用的 proxy
|
|
|
|
|
new = False
|
2020-12-07 00:27:55 +08:00
|
|
|
|
proxies = {'http': '127.0.0.1'}
|
2020-12-05 17:53:38 +08:00
|
|
|
|
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)}
|
2020-12-07 00:27:55 +08:00
|
|
|
|
result = requests.get(url, proxies=proxies).text
|
2020-12-09 00:18:32 +08:00
|
|
|
|
#hku_info("use proxy: {}".format(proxies['http']))
|
2020-12-07 00:27:55 +08:00
|
|
|
|
return result
|
2020-12-05 17:53:38 +08:00
|
|
|
|
except:
|
|
|
|
|
new = True
|
|
|
|
|
raise Exception("无法通过代理访问!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def request_with_local(url):
|
|
|
|
|
"""通过本机ip直接获取请求,访问失败将抛出异常"""
|
|
|
|
|
return requests.get(url).text
|