在我的HTTP请求中插入{“‘ccsrftoken”:client.cookie’‘ccsrftoken’}有一些问题。
其想法是使用X在我的防火墙上进行身份验证。
这是我的代码:
#!/usr/bin/env python
import requests
url = 'http://10.0.2.45/'
name = 'admin'
password = 'xyz'
#all cookies received will be stored in the session object
client = requests.session()
print 'client headers initially: ', client.headers, '\n'
#First connection used for authentication.
login = client.post(url + '/logincheck', data="username=" + name + "&secretkey=" + password, verify = False)
#csrftoken to be inserted in the headers for next put,post,delete requests.
This will be stored in csrftoken variable.
print 'client cookies after login: ', client.cookies, '\n \n'
print 'csrftoken value extracted from the cookie: ',
client.cookies['ccsrftoken'], '\n \n'
#we update the session headers with X-CSRFTOKEN
client.headers.update({"X-CSRFTOKEN": client.cookies['ccsrftoken']})
#Simple post command (empty, just to test authentication)
api_cmdb = 'api/v2/cmdb/'
c = client.post(url + api_cmdb + 'firewall/address?vdom=root', verify = False)在我的防火墙上,这将导致以下三个错误:
httpsd 160 - 1502297425错误is_valid_csrf_token3015 -- CSRF令牌失配
httpsd 160 - 1502297425错误api_cmdb_execute_handler1422 --没有找到有效的CSRF令牌
引发的httpsd 160 - 1502297425错误api_return_http_result528 -- API错误403
使用Wireshark并与CURL命令进行比较(这很好),我可以看到插入在“X”中的值具有双引号。例如,client.headers.update的输出({“‘ccsrftoken”:client.cookie’CSRFTOKEN‘}):
CaseInsensitiveDict({'X-CSRFTOKEN': '"6C369B52B8211679DF2AC9676945CC"', 'Accept-Encoding': 'gzip, deflate, compress', 'Accept': '*/*', 'User-Agent': 'python-requests/2.2.1 CPython/2.7.6 Linux/3.4.0+'})然而,应该简单地插入这个值,而不需要第二个“引号集”。
知道怎么纠正吗?
非常感谢
下面是来自新sess的client.cookies的输出:
<<class 'requests.cookies.RequestsCookieJar'>
[<Cookie APSCOOKIE_9539865664988587055="Era%3D0%26Payload%3DGAytA5jioAyuHvus1rw3dfKdzWrJm3CyraiFVxenLzBRb6qHLqlcnIIUaZz5ZJma%0A7MyKPN+4hgCPi8+yGeMhLdTVAAlG0zHmtPw7y6v+nrJVc1g7NZisFowGZ4TZacfL%0AaiMjHE+0MuJLA7r6COt4G+ikwMWlh8YWO0RF5rvE0t6nYX%2FLvla1yFjKy5Odu7kA%0AeewY6sB0zbybh6eRSWQf5Q%3D%3D%0A%26AuthHash%3DLolbIaWtHofmkwMG1Fh6gWc6K%2FkA%0A"
for 10.0.2.45/>,
<Cookie ccsrftoken="2A28F281C83FF4B3235134C335D53B5" for 10.0.2.45//>,
<Cookie ccsrftoken_9539865664988587055="2A28F281C83FF4B3235134C335D53B5" for 10.0.2.45//>]>发布于 2017-08-21 13:11:03
我找到了解决办法。
它只需要一个字符串切片。
如果a=client.cookie‘csrftoken’,那么csrftoken= a1:-1
测试后,这个功能很好。
https://stackoverflow.com/questions/45596908
复制相似问题