I am trying to login a web, at first i tried the requests.session:
import requests
session=requests.Session()
params={"username":"woniu","password":"password"}
s=session.post(url,params)
print(s.cookies.get_dict())
but login in failed, and there are no cookies return.(I thought maybe this web is a HTTP basic aceess authentication, i can only use auth)
then I tried to use auth of requests
from requests.auth import AuthBase
from requests.auth import HTTPBasicAuth
auth=HTTPBasicAuth('woniu','password')
r=requests.post(url",auth=auth)
this time login in succeed, but still no cookies return.
the problems is: even i login in succeed, when i try to post something, i found myself return to the login page. is this because i did not handle the cookies? how can i login in successfully and get the cookies at the same time.
If you know for sure that the page:
1. Is HTTPAuth protected
2. Expects a POST request
Then this should work (tested):
>>> import requests
>>> url = 'http://.../auth'
>>> r = requests.post(url, data={'post_data': '987654321'},
... auth=('user123', 'pass456'))
>>> r
<Response [200]>
>>> r.cookies.items()
[('secret_cookie', '987654321')]
Note that POST data is provided with data
argument, and HTTP Auth
credentials are provided simply as a tuple auth
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.