我使用Python请求库将一个JSON请求发送到运行在我的Raspberry Pi上的一个泛滥的Web torrentbox。
import requests
payload='{"method": "auth.login", "params": ["MY-PASSWORD"], "id": 1}'
r = requests.post("http://192.168.0.104:8112/json", data=payload)
print r.headers
print r.text
payload2='{"method": "core.add_torrent_magnet", "params": ["MAGNET-URI", {}], "id": 2}'
q = requests.post("http://192.168.0.104:8112/json", data=payload2)
print q.headers
print q.text
print "---------------"在运行上述代码时,我获得了成功的身份验证,但我的第二个请求出现了一个错误,即“未验证”。
输出
{“内容-编码”:“gzip”,“传输-编码”:“分块”,'Set-Cookie':'_session_id=d888b2548fd6966490eef4b3657a1b342169;Expires=Sun,2016年5月8日14:22:45 GMT;Path=/json','Server':'TwistedWeb/12.0.0','Date':'Sun,2016年5月8日13:22:45 GMT',‘内容-类型’:'application/x-json'} {id: 1,“结果”:true,“Server”:}null
{“传输-编码”:“分组”、“日期”:“Sun,2016年5月8日13:22:45格林尼治时间”、“服务器”:“TwistedWeb/12.0.0”、“内容-类型”、“内容-编码”:“内容-编码”:'gzip'} {"id":2、“结果”:空、“错误”:{“消息”:“未验证”、“代码”:1}}
如果我使用curl运行第二个请求,它就能工作。
curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "core.add_torrent_magnet", "params": ["MAGNET_URI", {}], "id": 1}' http://localhost:8112/json发布于 2016-05-08 14:19:10
明白了。会话cookie没有被共享,因为我使用不同的对象运行每个请求。使用以下方法启动会话:
r=requests.session()然后只在r上调用POST请求来运行每个请求。
https://stackoverflow.com/questions/37100561
复制相似问题