Reply

Cookie Authentication Using Python

New Member
Posts: 3
8506     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.

Re: Cookie Authentication Using Python

New Member
Posts: 3
8507     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

New Member
Posts: 3
8507     0

Corrected the closing of the session:

 

#Logout of the session
session.cookies.clear()
session.close()
Showing results for 
Search instead for 
Did you mean: 

Recommended for You