Skip to content Skip to sidebar Skip to footer

Http Basic Authentication, Using Python

I want my users to go to a protected directory on my domain. Both .htaccess and .htpasswd are created and reside in the protected library. The html that asks for a username/passwor

Solution 1:

The returned object from a urlopen call is much like an open file stream, you need to read it to get the output.

Change print result to print result.read():

result = urllib2.urlopen(request)

print"Content-type: text/html\n\n"print result.read()

Or, change result = urllib2.urlopen(request) to result = urllib2.urlopen(request).read():

result = urllib2.urlopen(request).read()

print"Content-type: text/html\n\n"print result

Check out these examples: http://docs.python.org/library/urllib2.html#examples

lunchbox

Solution 2:

When you write :

resource = urllib2.urlopen(url)
# Here resource is your handle to the url# resource provides a read function that mimics file read.

So, resource.read() # Reads the url like a file.

print resource # prints the repr for the resource object and not the actual content.

Post a Comment for "Http Basic Authentication, Using Python"