1 Star 0 Fork 0

hpchao / ReportIP

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BaseFunc.py 2.96 KB
一键复制 编辑 原始数据 按行查看 历史
hpchao 提交于 2017-11-18 17:36 . 初始化
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import uuid
import urllib.parse
import hmac
import hashlib
import base64
from datetime import datetime
from dateutil import tz
import myConfig
import ProxyBY
def DNS_Return(key):
'''
所有有关DNS的请求通过这里发出
:param key:
:return:
'''
return __getReturn(key,myConfig.DNSgetUrl)
def __getReturn(key,myurl):
if net_State(LocalNet()):
print(u'使用本地连接')
mystr=__get_QueryStr(key)
myurl+=mystr
r=LocalNet(myurl)
r.encoding=myConfig.ENCODING
return r.text
if net_State(ProxyNet()):
print(u'使用代理连接')
mystr=__get_QueryStr(key)
myurl+=mystr
r=ProxyNet(myurl)
r.encoding=myConfig.ENCODING
return r.text
return ''
def __get_QueryStr(inputDic):
inputDic['Format']=myConfig.retFormat
inputDic['Version']=myConfig.version
inputDic['SignatureMethod']=myConfig.SignatureMethod
inputDic['SignatureNonce']=str(uuid.uuid4())
inputDic['SignatureVersion']=myConfig.SignatureVersion
inputDic['AccessKeyId']=myConfig.accesskeyID
inputDic['Timestamp']=__get_ISO8601utc()
#print(inputDic)
stringToSign = __get_stringToSign(inputDic)
#print(stringToSign)
signature=__get_sha1(stringToSign)
inputDic['Signature'] = signature
#print(urllib.parse.urlencode(inputDic))
return urllib.parse.urlencode(inputDic)
def __get_ISO8601utc():
# t1=time.strftime('%Y-%m-%dT%H:%M:%SZ',time.localtime(time.time()))
# print(t1)
# UTC Zone
from_zone = tz.gettz(myConfig.TimeZone)
utc = datetime.utcnow()
# Tell the datetime object that it's in UTC time zone
utc = utc.replace(tzinfo=from_zone)
t = datetime.strftime(utc, myConfig.TimeFormat)
#print(t)
return t
def __get_stringToSign(inputDic):
newkey = sorted(inputDic.items(), key=lambda x: x[0])
#print('newkey:',newkey)
canstring = ''
for k, v in newkey:
canstring += '&' + __percentEncode(k) + '=' + __percentEncode(v)
#print(canstring)
stringToSign = myConfig.HTTP_METHOD + myConfig.SEPARATOR + '%2F&' + __percentEncode(canstring[1:])
return stringToSign
def __get_sha1(stringToSign):
Token = bytes(myConfig.accesspasswd + "&", encoding=myConfig.ENCODING)
Data = bytes(stringToSign, encoding=myConfig.ENCODING)
h = hmac.new(Token, Data, hashlib.sha1).digest()
signature = base64.encodebytes(h).strip()
signature = str(signature, encoding=myConfig.ENCODING)
return signature
def __percentEncode(value):
value=str(value)
res = urllib.parse.quote(value)
res = res.replace('+', '%20')
res = res.replace('*', '%2A')
res = res.replace('%7E', '~')
return res
def net_State(req):
req.encoding = 'gb2312'
#print(req.text)
if '您的' in req.text:
return True
return False
def ProxyNet(url=myConfig.ProxyTestURL):
p = ProxyBY.myProxy()
p.CreatProxyHandler()
req = p.ProxyUrl(url)
return req
def LocalNet(url=myConfig.ProxyTestURL):
req=requests.get(url)
return req
#print(ProxyNet().text)
Python
1
https://gitee.com/hpch/ReportIP.git
git@gitee.com:hpch/ReportIP.git
hpch
ReportIP
ReportIP
master

搜索帮助