login with python requests

requests library is a powerful python package for http. You can use it to fetch web pages, and do anything as the http verbs can do. To use requests, install it first:

pip install requests

The requests package will be installed in, e.g., c:\Python\Lib\site-packages\requests. To use the request package in  a script, import it first:

import requests

Since requests package imports its major functions/classes like request, get, head, post, patch, put, delete, options, Session in its __init__.py, we can use the apis directly such as:

import requests
r = requests.get('https://myprogrammingnotes.com')
r = requests.post('https://myprogrammingnotes.com', data = {'key':'value'})
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)

jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
r = requests.get(url, cookies=jar)

Logging in a website means you deliver a username/password to a url(login endpoint) in exchange for a cookie. You send the cookie in the subsequent http requests without the need to send the username/password again. The server decodes the cookie and tells that you have the privileges to access the resources.  To login a website, you’d better use the Session class of requests as Session class will preserve the states such as the cookie got from the login endpoint and send it automatically in the following requests. If you do not use Session, you need to save the cookie and send the cookie header in the subsequent requests manually, which is inconvenient.

import request
session = requests.Session()
session.headers.update({'User-Agent': "myapp1,0"})
mydata = json.dumps({'username': username,'password': password})
response = session.post(loginurl,data=mydata)

If you want to reuse the cookie after the program exits, you need to save the cookie into a file and load it from the file next time the program runs.

with open(cookiefilename, 'w') as f:
            json.dump(requests.utils.dict_from_cookiejar(session.cookies), f)

with open(cookiefilename, 'r') as f:
           session.cookies = requests.utils.cookiejar_from_dict(json.load(f))


If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply