Python Requests Module

Posted on
Mon Mar 14, 2016 9:08 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Python Requests Module

I've been spinning my wheels a bit on this, so I thought I'd reach out to folks smarter than me. I'm trying to access an API using requests.

Here is my (non)working code:
Code: Select all
import requests
from requests.auth import HTTPDigestAuth

requests.packages.urllib3.disable_warnings()

s = requests.session()

r = s.get('https://SERVER_IP', auth=HTTPDigestAuth('USERNAME', 'PASSWORD'), cookies={"new-cookie-identifier": "1234abcd"}, verify=False)
print r.content

payload = {'data': 'dhcp_leases'}
r1 = s.get('https://SERVER_IP/api/edge/data.json', params=payload, verify=False)
print r1.status_code
print r1.content

The first get works, and returns content (the idea being that this call would provide the session cookie.) The second get is where the rubber meets the road. However, the session doesn't seem to stay active and I get:
Code: Select all
403
{"success":false,"errors":["Permission denied"]}

I've tried many things, but none successful. I'm sure that it's something pretty simple, but I haven't been able to find workable examples to draw from. Hopefully, someone can point me in the right direction. :D

Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Mar 15, 2016 4:11 pm
jay (support) offline
Site Admin
User avatar
Posts: 18224
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python Requests Module

I don't know about the cookies, but it seems like you need to include the cookie in the second get or you'll lose the session, right? It'll think the connection is a new one.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Mar 15, 2016 5:23 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Requests Module

jay (support) wrote:
I don't know about the cookies, but it seems like you need to include the cookie in the second get or you'll lose the session, right? It'll think the connection is a new one.


Both requests are using the same session object ('s'), so I don't think that's absolutely necessary. But the examples I was looking at were inconclusive. And while I use the requests library in some of my code, I don't use the session object and I don't have any that use another get after the authenticating one. Hmmm.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Mar 15, 2016 7:13 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python Requests Module

Jay, I'm pretty sure you're right. I misread the docs:

Note, however, that method-level parameters will not be persisted across requests, even if using a session. This example will only send the cookies with the first request, but not the second:

@FlyingDiver: Thanks. I think I'm close, and I think my problems now are formatting the second request properly.

Here's where I'm at presently:
Code: Select all
import requests
from requests.auth import HTTPDigestAuth

requests.packages.urllib3.disable_warnings()

s = requests.session()
s.auth = ('USERNAME', 'PASSWORD')
s.get('https://10.0.1.1', verify=False)
r = s.get('https://10.0.1.1/api/edge/data.json', params={'data': 'dhcp_leases'}, verify=False)
print r.status_code
print r.text


Result:
Code: Select all
200

Fatal error: Cannot redeclare class [blah, blah, blah]


The '200' is encouraging, anyway...

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests