TypeError:无法连接' str'和“回应”#39;对象

时间:2015-07-20 10:19:27

标签: python

(忽略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

运行此代码时:

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作为变量。我不知道自己做错了什么。提前致谢,如果我没有清楚地解释清楚,请告诉我。

1 个答案:

答案 0 :(得分:2)

您正在尝试连接Response对象:

g = requests.get(token)
# ...
r.send('{"a":"auth","p":"'+g+'","t":'+t+'}')

g是响应对象。您想获得文本值

r.send('{"a": "auth", "p": "' + g.text + '", "t":' + t + '}')

如果您尝试在那里发送JSON数据,可能需要查看json module

r.send(json.dumps({'a': 'auth', 'p': g.text, 't': t}))