Python2.X代码转换成Python3.X代码

import http.lib
import json
conn = httplib.HTTPConnection("localhost:8080")
headers = {"Content-type":"application/json"} #application/x-www-form-urlencoded
params = ({"bindHyCardInfo":{"mobileNo":"1881026xxxx","userId":"2","cardno":"7926279367963021","cardpasswd":"xxxxxxxxxxxxxxx","ip":"127.0.0.1"},"header":{"version":"1.0.1","from":"1000","to":"2000","tid":"7926279367963021","time":"12312","token":"SEW342WEER2342","ext":""}})
conn.request("POST", "/core-oper/rest/bindCard", json.JSONEncoder().encode(params), headers)
response = conn.getresponse()
data = response.read()
if response.status == 200:
print ('success')
print (data)
else:
print ('fail')
conn.close()

import http.client
import json
conn = http.client.HTTPConnection("localhost:8080")
headers = {"Content-type":"application/json"} #application/x-www-form-urlencoded
params = ({"bindHyCardInfo":{"mobileNo":"1881026xxxx","userId":"2","cardno":"7926279367963021","cardpasswd":"xxxxxxxxxxxxxxx","ip":"127.0.0.1"},"header":{"version":"1.0.1","from":"1000","to":"2000","tid":"7926279367963021","time":"12312","token":"SEW342WEER2342","ext":""}})
conn.request("POST", "/core-oper/rest/bindCard", json.JSONEncoder().encode(params), headers)
response = conn.getresponse()
data = response.read()
if response.status == 200:
    print('success')
    print(data)
else:
    print('fail')
    conn.close()

你看看这行不行

追问

太感谢了,还能再麻烦您一下么?
我的http head是这样的
请求HTTP HEAD:
Authorization:Basic base64.encoding(appKey:appSecret)
Accept :application/json
Content-Type:application/json;charset=UTF-8
如何按照我的head对原代码进行修改呢?

追答

改这里
params = ({"bindHyCardInfo":{"mobileNo":"1881026xxxx","userId":"2","cardno":"7926279367963021","cardpasswd":"xxxxxxxxxxxxxxx","ip":"127.0.0.1"},"header":{"version":"1.0.1","from":"1000","to":"2000","tid":"7926279367963021","time":"12312","token":"SEW342WEER2342","ext":"", "Authorization":"Basic base64.encoding(appKey:appSecret)","Accept":"application/json","Content-Type":"application/json;charset=UTF-8"}})
你看看行不行吧

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答