Login using usrllib2

Chris Rebert clp2 at rebertia.com
Wed Dec 1 05:16:46 EST 2010


On Wed, Dec 1, 2010 at 1:53 AM,  <dudeja.rajat at gmail.com> wrote:
> Hi All,
>
>
> I'm using urllib2 module to login to an https server. However I'm unable to
> login as the password is not getting accepted.
>
> Here is the code:
>
> import urllib2, urllib
> values={'Username': 'admin', 'Password': 'admin123'}
> url='https://172.25.17.20:9443'
> data = urllib.urlencode(values)
>
>
> data = urllib.urlencode(values)
> req = urllib2.Request(url, data)
> response = urllib2.urlopen(req)
> the_page = response.read()
> print the_page
>
> The error message I get in the_page output is the same as I get when I
> access this site using a browser and supply a wrong password. It appears
> password is not accepting.
>
> I'm new to web programming. The aim is to automate user clicks on a web
> server which can be replicated using python and http. Please suggest / help.

You should probably use something like Firefox's "Live HTTP Headers"
extension to see what exactly the web browser is doing when you
normally login to the site, so that you can correctly replicate the
browser's actions in your script.
The site may be using HTTP Basic or HTTP Digest authentication, in
which case you'll need to use urllib2.HTTPBasicAuthHandler or
urllib2.HTTPDigestAuthHandler.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list