类型错误:不能连接“STR”和“响应”对象
问题描述:
(忽略g.text和p.content回报“你无权查看此内容”从plug.dj)我得到的错误类型错误:不能连接“STR”和“响应”对象
Traceback (most recent call last):
File "plugling.py", line 20, in <module>
r.send('{"a":"auth","p":"'+g+'","t":'+t+'}')
TypeError: cannot concatenate 'str' and 'Response' objects
运行此代码:
import time
from websocket import create_connection
import requests
import calendar
slug = 'sfoc'
r = create_connection("wss://godj.plug.dj/socket")
t = calendar.timegm(time.gmtime())
token = 'https://plug.dj/_/auth/token'
join = 'https://plug.dj/_/rooms/join'
pl = {'slug': 'sfoc'}
g = requests.get(token)
print g.text
p = requests.post(join, data=pl)
print p.content
r.send('{"a":"auth","p":"'+g+'","t":'+t+'}')
result = r.recv()
print result
r.close()
它没有用%s的变量要么喜欢我。我不知道我做错了什么。如果我没有清楚地说明,请提前致谢,并告诉我。
答
您正在试图连接一Response
对象:
g = requests.get(token)
# ...
r.send('{"a":"auth","p":"'+g+'","t":'+t+'}')
g
是响应对象。你想获得文本值:
r.send('{"a": "auth", "p": "' + g.text + '", "t":' + t + '}')
你可能想看看json
module如果你想有发送JSON数据:
r.send(json.dumps({'a': 'auth', 'p': g.text, 't': t}))
为错误说'g'是'响应'对象,所以你不能将它与一个字符串连接起来,你可以在这里使用'g.text' – Kasramvd