将 httplib 更改为 http.client
Change httplib to http.client
我收到错误 'No module named httplib'。然后我将 httplib
替换为 http.client。我用的是2to3,在secret_key
之前加了b
。
import http.client
import urllib.request, urllib.parse, urllib.error
import json
import hashlib
import hmac
from collections import OrderedDict
import time
server = "api.---.net"
api_key = "---"
secret_key = b"---"
def get(url):
conn = http.client.HTTPSConnection(server)
conn.request("GET", url)
response = conn.getresponse()
data = json.load(response)
return data
def post(url, params):
conn = http.client.HTTPSConnection(server)
data = OrderedDict(sorted(params))
encoded_data = urllib.parse.urlencode(data)
sign = hmac.new(secret_key, msg=encoded_data, digestmod=hashlib.sha256).hexdigest().upper()
headers = {"Api-key": api_key, "Sign": sign, "Content-type": "application/x-www-form-urlencoded"}
conn.request("POST", url, encoded_data, headers)
def com():
conn = http.client.HTTPSConnection(server)
sign = hmac.new(secret_key, b'', digestmod=hashlib.sha256).hexdigest().upper()
headers = {"Api-key": api_key, "Sign": sign, "Content-type": "application/x-www-form-urlencoded"}
conn.request("GET", "/ex/com", None, headers)
现在我得到错误
'NoneType' object is not subscriptable
Traceback (most recent call last):
File "lc.py", line , in <module>
COM = float(com()['fee'])
TypeError: 'NoneType' object is not subscriptable
函数com()
return什么都没有(也就是一个None
)。在 return 上,您尝试将选择运算符应用于 None
(['fee']
),这仅在 com()
returned 字典时有效。
我收到错误 'No module named httplib'。然后我将 httplib
替换为 http.client。我用的是2to3,在secret_key
之前加了b
。
import http.client
import urllib.request, urllib.parse, urllib.error
import json
import hashlib
import hmac
from collections import OrderedDict
import time
server = "api.---.net"
api_key = "---"
secret_key = b"---"
def get(url):
conn = http.client.HTTPSConnection(server)
conn.request("GET", url)
response = conn.getresponse()
data = json.load(response)
return data
def post(url, params):
conn = http.client.HTTPSConnection(server)
data = OrderedDict(sorted(params))
encoded_data = urllib.parse.urlencode(data)
sign = hmac.new(secret_key, msg=encoded_data, digestmod=hashlib.sha256).hexdigest().upper()
headers = {"Api-key": api_key, "Sign": sign, "Content-type": "application/x-www-form-urlencoded"}
conn.request("POST", url, encoded_data, headers)
def com():
conn = http.client.HTTPSConnection(server)
sign = hmac.new(secret_key, b'', digestmod=hashlib.sha256).hexdigest().upper()
headers = {"Api-key": api_key, "Sign": sign, "Content-type": "application/x-www-form-urlencoded"}
conn.request("GET", "/ex/com", None, headers)
现在我得到错误
'NoneType' object is not subscriptable
Traceback (most recent call last):
File "lc.py", line , in <module>
COM = float(com()['fee'])
TypeError: 'NoneType' object is not subscriptable
函数com()
return什么都没有(也就是一个None
)。在 return 上,您尝试将选择运算符应用于 None
(['fee']
),这仅在 com()
returned 字典时有效。