Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTP Authentication

Status
Not open for further replies.

glmrenard

Technical User
Oct 29, 2002
52
FR
Hello,

I have to get a web page with python but I can't ...
I don't know how to send login/password to authenticate.
Could someone give me an exemple please ?
 
Read the python maual pages for urllib2.

The basic construct is

Code:
        handler = urllib2.HTTPBasicAuthHandler()
        handler.add_password(realm, domain, webuser, webpass)
 
Hello,

Thanks for answering.
I have tried lot of thing and nothin work so here my final code (it works but Iuse httplib)
conn=httplib.HTTPConnection(self.host)
conn.request("GET" , self.url , "", {"Authorization":"Basic "+base64.encodestring(self.login+":"+self.password)} )
r=conn.getresponse()
self.data=r.read()

Do you have a complete exemple with urllib2 ?
 
From the manual for HTTPLib:

11.6 httplib -- HTTP protocol client

This module defines classes which implement the client side of the HTTP and HTTPS protocols. It is normally not used directly -- the module urllib uses it to handle URLs that use HTTP and HTTPS.

Here's an example:

Code:
import urllib2

handler = urllib2.HTTPBasicAuthHandler()
handler.add_password('AuthName from .htaccess',
    '[URL unfurl="true"]www.your.com',[/URL] 'yourusername', 'yourpass')

opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)

try:
    f = urllib2.urlopen( '[URL unfurl="true"]http://www.your.com/path'[/URL] )
except urllib2.HTTPError, e:
    if e.code == 401:
        print 'not authorized'
    elif e.code == 404:
        print 'not found'
    elif e.code == 503:
        print 'service unavailable'
    else:
        print 'unknown error: '
else:
    print 'success'
    for line in f:
        print line,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top