Question

I am trying to use httplib2 to log in to a web page. I am able to log in to the page by simply opening the following URL in a Chrome incognito window:

https://domain.com/auth?name=USERNAME&pw=PASSWORD

I tried the following code to emulate this login with httplib2:

from httplib2 import Http
h = Http(disable_ssl_certificate_validation=True)
resp, content = h.request('https://domain.com/auth?name=USERNAME&pw=PASSWORD')

Unfortunately, this request does not lead to a successful login.

I tried changing the request headers to match those provided by Chrome:

headers = {
    'Host': 'domain.com',
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
    'Accept-Encoding': 'gzip,deflate,sdch',
    'Accept-Language': 'en-US,en;q=0.8'
}
resp, content = h.request('https://domain.com/auth?name=USERNAME&pw=PASSWORD', 'GET', headers=headers)

This changes the response slightly, but still does not lead to a successful login.

I tried inspecting the actual network traffic with Wireshark but since it's HTTPS and thus encrypted, I can't see the actual traffic.

Does anybody know what the difference in requests between Chrome and httplib2 could be? Maybe httplib2 changes some of my headers?

Était-ce utile?

La solution

Following Games Brainiac's comment, I ended up simply using Python Requests instead of httplib2. The following requests code works out of the box:

import requests
session = requests.Session()
response = session.get('https://domain.com/auth?name=USERNAME&pw=PASSWORD')

Further requests with the same username/password can simply be performed on the Session object:

...
next_response = session.get('https://domain.com/someOtherPage')
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top