Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Cookie Authentication Using Python
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2023 09:26 AM
7909     0
Is there an example of using a cookie in a Python script to verify authentication? This is as far as I have gotten. The requests returns a 401 error indicating that there is not any authention.
response = requests.get(url, verify=False, headers=headers)
import json
import requests
from requests.structures import CaseInsensitiveDict
requests.packages.urllib3.disable_warnings()
gm_user = 'IPAM_Script'
gm_pwd = 'password'
network = "10.42.6.0"
session = requests.session()
response_login = requests.get(url, verify=False, auth=(gm_user, gm_pwd))
headers = CaseInsensitiveDict()
headers["Cookie"] = response_login.cookies['ibapauth']
response = requests.get(url, verify=False, headers=headers)
net_obj = json.loads(response.text)
response_logout = requests.put(url, verify=False,headers=headers)
exit()
Any help will be appreciated.
Thanks.
Solved! Go to Solution.
Re: Cookie Authentication Using Python
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2023 12:39 PM
7910     0
I found the solution:
import json
import requests
requests.packages.urllib3.disable_warnings()
gm_user = 'IPAM_Script'
gm_pwd = 'Password'
network = "10.42.6.0"
#Open a session
session = requests.session()
#Logon to the Grid
session = requests.get(url, verify=False, auth=(gm_user, gm_pwd))
#Get a network using the session for authentication
response = requests.get(url, verify=False, cookies=session.cookies)
net_obj = json.loads(response.text)
print(net_obj[0]['network'])
#Logout of the session
response_logout = requests.put(url, verify=False,cookies=session.cookies)
exit()
Re: Cookie Authentication Using Python
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2023 03:10 PM
7910     0
Corrected the closing of the session:
#Logout of the session
session.cookies.clear()
session.close()