(忽略 g.text 和 p.content 从plug.dj 返回“您无权查看此内容”)我收到错误
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
Run Code Online (Sandbox Code Playgroud)
运行此代码时:
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()
Run Code Online (Sandbox Code Playgroud)
它也不喜欢我使用 %s 作为变量。我不知道我做错了什么。提前致谢,如果我没有解释清楚,请告诉我。
您正在尝试连接一个Response对象:
g = requests.get(token)
# ...
r.send('{"a":"auth","p":"'+g+'","t":'+t+'}')
Run Code Online (Sandbox Code Playgroud)
g是响应对象。您想要获取文本值:
r.send('{"a": "auth", "p": "' + g.text + '", "t":' + t + '}')
Run Code Online (Sandbox Code Playgroud)
如果您尝试向该json模块发送 JSON 数据,您可能需要查看该模块:
r.send(json.dumps({'a': 'auth', 'p': g.text, 't': t}))
Run Code Online (Sandbox Code Playgroud)