出现 TypeError: string argument expected, got 'bytes' 解决方案
在使用StringIO的write方法上,用BytesIO替代StringIO即可解决
b.getvalue
默认返回值的前面,有一个'b'代表的是bytes类型
以下方法过滤了'b'
import pycurl
import json
from io import *
def _curlPost(api,method,params):
logging.debug(method)
logging.debug(params)
url = 'localhost:8443/'+api
# params = {"foo":"hello ", "bar":"world"};
data = json.dumps({"jsonrpc": "2.0", "method": method,"params":params, "id": 1})
body = BytesIO() #StringIO()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.WRITEFUNCTION, body.write)
curl.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json']);
curl.setopt(pycurl.POST, 1)
curl.setopt(pycurl.POSTFIELDS, data)
curl.perform()
html = body.getvalue()
print(body)
print ("HTTP-code:", curl.getinfo(curl.HTTP_CODE))
body.close()
curl.close()
temp = None
if html==None:
print('yes')
else:
print('no')
temp=html.decode()
print(temp)
return temp
调用例子
ret = _curlPost("users","get_mlisturl",userJson)
print (ret)
网友评论