如何使用请求执行 HTTP/XML 身份验证
How to perform an HTTP/XML authentication with requests
我正在尝试使用请求 2.7 通过 Python 3.4 向 Docushare 进行身份验证。我对 Python 和请求模块还比较陌生,但我已经阅读了大量的资料,但无法取得更多进展。我的代码没有给出任何错误,并且我从 post 请求中收到了一个 JSESSIONID cookie,但我没有收到 AmberUser 身份验证 cookie。我不知道如何进一步调查以找出问题所在。
请求的形式来自http://docushare.xerox.com/en-us/Help/prog/prog5.htm#Authentication
要求:
POST /dscgi/ds.py/Login HTTP/1.1
Host: docushare.xerox.com
Content-Type: text/xml
Content-Length: xxxx
<?xml version="1.0" ?>
<authorization>
<username>msmith</username>
<password>mysecretstring</password>
</authorization>
我的 Python / 请求代码如下所示:
import requests
url = "https://mydocusharesite/dsweb/Login"
xml="""<?xml version='1.0' ?>
<authorization>
<username>myusername</username>
<password><![CDATA[mypa$$word]]></password>
<domain>DocuShare</domain>
</authorization>"""
headers = {"DocuShare-Version":"5.0", 'Content-Type':'text/xml'}
s = requests.Session()
r = s.post(url,data=xml,headers=headers)
print('Status code:', r.status_code)
print('headers:\n', r.headers)
print('request headers:\n',r.request.headers)
c = s.cookies
print('Cookies:\n', c)
我得到的输出是
Status code: 200
headers:
{'set-cookie': 'JSESSIONID=21B7E5E0D83D1F1267371B9FD1B19BBC.tomcat1; Path=/docushare; Secure', 'transfer-encoding': 'chunked', 'connection': 'close', 'content-type': 'text/html;charset=UTF-8', 'cache-control': 'private', 'date': 'Sun, 07 Jun 2015 02:22:59 GMT', 'expires': '-1'}
request headers:
{'Connection': 'keep-alive', 'DocuShare-Version': '5.0', 'Accept': '*/*', 'Content-Type': 'text/xml', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '153', 'User-Agent': 'python-requests/2.7.0 CPython/3.4.3 Darwin/14.3.0'}
Cookies:
<RequestsCookieJar[<Cookie JSESSIONID=21B7E5E0D83D1F1267371B9FD1B19BBC.tomcat1 for mydocusharesite>]>
您的 CDATA 部分应该类似于 <![CDATA[mypa$$word]]>
。您的代码当前正在发送 ![CDATA[mypa$$word]]
作为实际密码。
我正在尝试使用请求 2.7 通过 Python 3.4 向 Docushare 进行身份验证。我对 Python 和请求模块还比较陌生,但我已经阅读了大量的资料,但无法取得更多进展。我的代码没有给出任何错误,并且我从 post 请求中收到了一个 JSESSIONID cookie,但我没有收到 AmberUser 身份验证 cookie。我不知道如何进一步调查以找出问题所在。
请求的形式来自http://docushare.xerox.com/en-us/Help/prog/prog5.htm#Authentication
要求:
POST /dscgi/ds.py/Login HTTP/1.1
Host: docushare.xerox.com
Content-Type: text/xml
Content-Length: xxxx
<?xml version="1.0" ?>
<authorization>
<username>msmith</username>
<password>mysecretstring</password>
</authorization>
我的 Python / 请求代码如下所示:
import requests
url = "https://mydocusharesite/dsweb/Login"
xml="""<?xml version='1.0' ?>
<authorization>
<username>myusername</username>
<password><![CDATA[mypa$$word]]></password>
<domain>DocuShare</domain>
</authorization>"""
headers = {"DocuShare-Version":"5.0", 'Content-Type':'text/xml'}
s = requests.Session()
r = s.post(url,data=xml,headers=headers)
print('Status code:', r.status_code)
print('headers:\n', r.headers)
print('request headers:\n',r.request.headers)
c = s.cookies
print('Cookies:\n', c)
我得到的输出是
Status code: 200
headers:
{'set-cookie': 'JSESSIONID=21B7E5E0D83D1F1267371B9FD1B19BBC.tomcat1; Path=/docushare; Secure', 'transfer-encoding': 'chunked', 'connection': 'close', 'content-type': 'text/html;charset=UTF-8', 'cache-control': 'private', 'date': 'Sun, 07 Jun 2015 02:22:59 GMT', 'expires': '-1'}
request headers:
{'Connection': 'keep-alive', 'DocuShare-Version': '5.0', 'Accept': '*/*', 'Content-Type': 'text/xml', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '153', 'User-Agent': 'python-requests/2.7.0 CPython/3.4.3 Darwin/14.3.0'}
Cookies:
<RequestsCookieJar[<Cookie JSESSIONID=21B7E5E0D83D1F1267371B9FD1B19BBC.tomcat1 for mydocusharesite>]>
您的 CDATA 部分应该类似于 <![CDATA[mypa$$word]]>
。您的代码当前正在发送 ![CDATA[mypa$$word]]
作为实际密码。