Skip to content

Instantly share code, notes, and snippets.

@SilvesterHsu
Created January 4, 2018 05:07
Show Gist options
  • Save SilvesterHsu/fc2d781bbe4dc583ce278d5271e4f87c to your computer and use it in GitHub Desktop.
Save SilvesterHsu/fc2d781bbe4dc583ce278d5271e4f87c to your computer and use it in GitHub Desktop.
Rewrite `httpGet` function
#!/usr/bin/python
# -*- coding: utf-8 -*-
import http.client
import urllib
import json
from hashlib import sha512
import hmac
def getSign(params,secretKey):
sign = ''
for key in (params.keys()):
sign += key + '=' + str(params[key]) +'&'
sign = sign[:-1]
my_sign = hmac.new( bytes(secretKey,encoding='utf8'),bytes(sign,encoding='utf8'), sha512).hexdigest()
return my_sign
def httpGet(url,resource,params=''):
# tackle sslerror
import urllib3
http = urllib3.PoolManager()
response = http.request('GET',url+resource + '/' + params)
data = response.data.decode('utf-8')
return json.loads(data)
def httpPost(url,resource,params,apikey,secretkey):
headers = {
"Content-type" : "application/x-www-form-urlencoded",
"KEY":apikey,
"SIGN":getSign(params,secretkey)
}
conn = http.client.HTTPSConnection(url, timeout=10)
if params:
temp_params = urllib.parse.urlencode(params)
else:
temp_params = ''
print(temp_params)
conn.request("POST", resource, temp_params, headers)
response = conn.getresponse()
data = response.read().decode('utf-8')
params.clear()
conn.close()
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment