I've been trying to update a small Python library called libpynexmo to work with Python 3.
我一直在尝试更新一个名为libpynexmo的小Python库,以便使用Python 3。
I've been stuck on this function:
我被困在这个功能上:
def send_request_json(self, request):
url = request
req = urllib.request.Request(url=url)
req.add_header('Accept', 'application/json')
try:
return json.load(urllib.request.urlopen(req))
except ValueError:
return False
When it gets to this, json responds with:
当它达到这个值时,json响应为:
TypeError: the JSON object must be str, not 'bytes'
I read in a few places that for json.load
you should pass objects (In this case an HTTPResponse
object) with a .read()
attached, but it doesn't work on HTTPResponse
objects.
我在一些地方读到json。加载您应该传递对象(在本例中是HTTPResponse对象)并附带一个.read(),但是它不能在HTTPResponse对象上工作。
I'm at a loss as to where to go with this next, but being that my entire 1500 line script is freshly converted to Python 3, I don't feel like going back to 2.7.
我不知道下一步该怎么做,但是由于我的整个1500行脚本都是刚转换成Python 3的,所以我不想回到2.7。
4 个解决方案
#1
17
I recently wrote a small function to send Nexmo messages. Unless you need the full functionality of the libpynexmo code, this should do the job for you. And if you want to continue overhauling libpynexmo, just copy this code. The key is utf8 encoding.
我最近写了一个小函数来发送Nexmo消息。除非您需要libpynexmo代码的完整功能,否则这将为您完成这项工作。如果您想继续检查libpynexmo,只需复制此代码。关键是utf8编码。
If you want to send any other fields with your message, the full documentation for what you can include with a nexmo outbound message is here
如果您想要在消息中发送任何其他字段,那么可以在nexmo出站消息中包含的内容的完整文档就在这里
Python 3.4 tested Nexmo outbound (JSON):
Python 3.4测试的Nexmo出站(JSON):
def nexmo_sendsms(api_key, api_secret, sender, receiver, body):
"""
Sends a message using Nexmo.
:param api_key: Nexmo provided api key
:param api_secret: Nexmo provided secrety key
:param sender: The number used to send the message
:param receiver: The number the message is addressed to
:param body: The message body
:return: Returns the msgid received back from Nexmo after message has been sent.
"""
msg = {
'api_key': api_key,
'api_secret': api_secret,
'from': sender,
'to': receiver,
'text': body
}
nexmo_url = 'https://rest.nexmo.com/sms/json'
data = urllib.parse.urlencode(msg)
binary_data = data.encode('utf8')
req = urllib.request.Request(nexmo_url, binary_data)
response = urllib.request.urlopen(req)
result = json.loads(response.readall().decode('utf-8'))
return result['messages'][0]['message-id']
#2
43
Facing the same problem I solve it using decode()
面对同样的问题,我使用decode()
...
rawreply = connection.getresponse().read()
reply = json.loads(rawreply.decode())
#3
8
I met the problem as well and now it pass
我也遇到了这个问题,现在它过去了
import json
import urllib.request as ur
import urllib.parse as par
html = ur.urlopen(url).read()
print(type(html))
data = json.loads(html.decode('utf-8'))
#4
2
Since you are getting a HTTPResponse, you can use Tornado.escape and its json_decode()
to convert the JSON strign into a dictionary:
由于您得到了一个HTTPResponse,您可以使用旋风.escape和它的json_decode()将JSON strign转换成字典:
from tornado import escape
body = escape.json_decode(body)
From the manual:
从手册:
tornado.escape.json_decode(value)
tornado.escape.json_decode(值)
Returns Python objects for the given JSON string.
返回给定JSON字符串的Python对象。
#1
17
I recently wrote a small function to send Nexmo messages. Unless you need the full functionality of the libpynexmo code, this should do the job for you. And if you want to continue overhauling libpynexmo, just copy this code. The key is utf8 encoding.
我最近写了一个小函数来发送Nexmo消息。除非您需要libpynexmo代码的完整功能,否则这将为您完成这项工作。如果您想继续检查libpynexmo,只需复制此代码。关键是utf8编码。
If you want to send any other fields with your message, the full documentation for what you can include with a nexmo outbound message is here
如果您想要在消息中发送任何其他字段,那么可以在nexmo出站消息中包含的内容的完整文档就在这里
Python 3.4 tested Nexmo outbound (JSON):
Python 3.4测试的Nexmo出站(JSON):
def nexmo_sendsms(api_key, api_secret, sender, receiver, body):
"""
Sends a message using Nexmo.
:param api_key: Nexmo provided api key
:param api_secret: Nexmo provided secrety key
:param sender: The number used to send the message
:param receiver: The number the message is addressed to
:param body: The message body
:return: Returns the msgid received back from Nexmo after message has been sent.
"""
msg = {
'api_key': api_key,
'api_secret': api_secret,
'from': sender,
'to': receiver,
'text': body
}
nexmo_url = 'https://rest.nexmo.com/sms/json'
data = urllib.parse.urlencode(msg)
binary_data = data.encode('utf8')
req = urllib.request.Request(nexmo_url, binary_data)
response = urllib.request.urlopen(req)
result = json.loads(response.readall().decode('utf-8'))
return result['messages'][0]['message-id']
#2
43
Facing the same problem I solve it using decode()
面对同样的问题,我使用decode()
...
rawreply = connection.getresponse().read()
reply = json.loads(rawreply.decode())
#3
8
I met the problem as well and now it pass
我也遇到了这个问题,现在它过去了
import json
import urllib.request as ur
import urllib.parse as par
html = ur.urlopen(url).read()
print(type(html))
data = json.loads(html.decode('utf-8'))
#4
2
Since you are getting a HTTPResponse, you can use Tornado.escape and its json_decode()
to convert the JSON strign into a dictionary:
由于您得到了一个HTTPResponse,您可以使用旋风.escape和它的json_decode()将JSON strign转换成字典:
from tornado import escape
body = escape.json_decode(body)
From the manual:
从手册:
tornado.escape.json_decode(value)
tornado.escape.json_decode(值)
Returns Python objects for the given JSON string.
返回给定JSON字符串的Python对象。