2

I'm trying to get the sessionId, so i can do other requests.
So i looked in the Firefox Network monitor (Ctrl+Shift+Q) and saw this:

enter image description here

So i wondered how i could do the request in python 3 and tried things like this:

import requests
payload = {'uid' : 'username',
      'pwd' : 'password'}

r = requests.get(r'http://192.168.2.114(cgi-bin/wwwugw.cgi', data=payload)

print r.text

But I always get "Response [400]".
If the request is correct, I should get something like this:

enter image description here

Thanks
Alex

4
  • It should be requests.post. and you have to pass every item using the same format.meaning, you have to include method, params parts as well. also make sure to enter the correct url. the one in your question has a typo. Commented May 9, 2017 at 15:03
  • @Himal so I did enter the method param aswell and now i'm getting Errno 133 (payload = {'method':'ugw-login', 'params':{'uid' : 'gw', 'pwd' : 'GATEWAY'}}) Commented May 10, 2017 at 7:09
  • Could you please update your question to include the latest version of the code you have and the complete error message you are getting ? Commented May 10, 2017 at 12:31
  • Also, can you see any cookies being sent on that POST request ? Additionally, you might have to include other request headers such as "Referer". you can test it by removing one of the headers and posting it again (using the tool you used on your first image) . do this with every header. Commented May 10, 2017 at 12:38

3 Answers 3

3

Just use a session, which will handle redirects and cookies for you:

import requests
payload = {'uid' : 'username',
          'pwd' : 'password'}

with requests.Session() as session:
    r = session.post(r'http://192.168.2.114(cgi-bin/wwwugw.cgi', data=payload)

    print(r.json)

This way you don't explicitly need to get the sessionId, but if you still want to, you can access the returned JSON as a dictionary.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer, well it's not working, I'm getting Errno 133. Any other way i could do this?
2

if you want to get the Session ID you can use Session() from the requests library:

URL = "Some URL here"

client = requests.Session()
client.get(URL)
client.cookies['sessionid']

1 Comment

In requests 2.32.4 this results in a KeyError exception
1

Although it's not very clear from your question, but I've noticed few issues with what you are trying to accomplish.

  1. If you are using session authentication, then you are suppose the send session_id as a Cookie header, which you aren't doing.

  2. 400 response code means bad request, not authentication required. Why are you sending data in get request to begin with? There's a difference between data and query params.

2 Comments

Oh yes, forgot to say that i'm a noob in Python
Yeah all of us commit mistakes like that. You need to frame your question a little better though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.