How to login https sever with inputing account name and password?

Shashwat Anand anand.shashwat at gmail.com
Thu Mar 4 13:54:27 CET 2010


You may also want to look into mechanize module.

On Thu, Mar 4, 2010 at 6:11 PM, Michael Rudolf <spamfresser at ch3ka.de> wrote:

> Am 04.03.2010 11:38, schrieb Karen Wang:
>
>  Hi all,
>>
>> I want to use python to access to https server, like
>> "https://212.218.229.10/chinatest/"
>>
>> If open it from IE, will see the pop-up login windows like this
>>
>>
>>
>> I tried several ways but always only get page for" HTTP Error 401.2 -
>> Unauthorized" error. ( myusername and mypassword are all correct)
>>
>> Below is my code:
>>
>> import urllib2
>>
>> values = {
>>
>>     'user' : "myusername",
>>
>> "pass' : "mypassword" }
>>
>> data = urllib2.urlencode(values)
>>
>> t = urllib2.urlopen('https://212.218.229.10/chinatest/',data)
>>
>> print t.read()
>>
>> where I am wrong ?
>>
>
> AFAIR does urlopen() expect the password to be Base64-encoded, not
> urlencoded.
>
> You might also need to add an AUTH-Line. But this all does not matter, as
> there is more than one AUTH-Method you'd have to implement them all, but
> fortunately urllib2.HTTPBasicAuthHandler and urllib2.HTTPBasicAuthHandler
> exist.
>
> So use them:
>
> import urllib2
> passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
> passman.add_password(None, theurl, username, password)
> authhandler = urllib2.HTTPBasicAuthHandler(passman)
> opener = urllib2.build_opener(authhandler)
> urllib2.install_opener(opener)
> pagehandle = urllib2.urlopen(theurl)
>
> (taken from
> http://www.voidspace.org.uk/python/articles/authentication.shtml )
>
> Further reference:
> http://www.python.org/doc/2.5.2/lib/module-urllib2.html
> http://www.python.org/doc/2.5.2/lib/urllib2-examples.html
>
> HTH,
> Michael
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100304/d4aa75b4/attachment.html>


More information about the Python-list mailing list