将httplib更改为http.client

时间:2018-02-16 05:15:52

标签: python httplib

我收到错误'没有名为httplib的模块'。然后我将httplib替换为http.client。我使用了2to3并在b之前添加了secret_key

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'对象不可订阅

Traceback (most recent call last):
  File "lc.py", line , in <module>
    COM = float(com()['fee'])
TypeError: 'NoneType' object is not subscriptable

1 个答案:

答案 0 :(得分:1)

函数com()不返回任何内容(即None)。返回时,您尝试将选择运算符应用于None['fee']),这仅在com()返回字典时才有效。