[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

Karen Wang

3/4/2010 10:38:00 AM

Hi all,

I want to use python to access to https server, like
"https://212.218.229.10/chinat...

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/...)

print t.read()

where I am wrong ?

Thank you very much.





Best Regards

Karen Wang



1 Answer

Michael Rudolf

3/4/2010 12:41:00 PM

0

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/chinat...
>
> 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/...)
>
> 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/authentica... )

Further reference:
http://www.python.org/doc/2.5.2/lib/module-ur...
http://www.python.org/doc/2.5.2/lib/urllib2-exa...

HTH,
Michael