How to login https sever with inputing account name and password?
Michael Rudolf
spamfresser at ch3ka.de
Thu Mar 4 07:41:22 EST 2010
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
More information about the Python-list
mailing list