2015-07-20 83 views
1

(忽略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的變量要麼喜歡我。我不知道我做錯了什麼。如果我沒有清楚地說明,請提前致謝,並告訴我。

+1

爲錯誤說'g'是'響應'對象,所以你不能將它與一個字符串連接起來,你可以在這裏使用'g.text' – Kasramvd

回答

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 module如果你想有發送JSON數據:

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