在请求中基于cookie的持久会话

时间:2015-08-12 12:09:23

标签: python python-2.7 session python-3.x python-requests

为什么我的会话在完成后仍然需要我进行身份验证?

openDialog()

为什么我的会话不持久?

之后打印控制台:

def main(pswd):
    with session() as s:
        s.post('url',
               auth=('user', pswd),
              )

        response = s.get('url2', cookies=s.cookies)

        print(response.text)

我也可以这样做:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

这将有效,但对于第二个 s.get ,它将无效。我必须再次通过证书。

1 个答案:

答案 0 :(得分:1)

对请求使用Session object时,您可以按如下方式设置身份验证详细信息:

with session() as s:
    s.auth = ('user', 'pass')
    s.post(url)  # auth headers will be sent with this request
    s.post(url2)  auth headers will be sent with this request as well