htaccess & urllib

R

Rene Pijlman

Max Khesin:
Is there a way to access an htaccess-protected directory with urllib,
password being known?

..htaccess is not a protection mechanism, but a configuration
file. If this configuration file specifies HTTP Basic
Authentication you can use:

"class FancyURLopener(...)
basic HTTP authentication is performed ...
Note: When performing basic authentication, a FancyURLopener
instance calls its prompt_user_passwd() method. The default
implementation asks the users for the required information on
the controlling terminal. A subclass may override this method to
support more appropriate behavior if needed."
http://www.python.org/doc/current/lib/module-urllib.html
 
J

John J. Lee

Max Khesin said:
Is there a way to access an htaccess-protected directory with urllib,
password being known?

urllib2 is better than urllib.

From the urllib2 module docstring, cut down a bit (and assuming you
mean basic HTTP authentication -- HTTPDigestAuthHandler might also be
of use to you):

import urllib2

authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password('realm', 'host', 'username', 'password')

opener = urllib2.build_opener(authinfo)

f = opener.open('http://www.python.org/')

print f.info() # response headers
print f.read() # response body


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top