[Zope] Using urllib2 to access Zope URLs

Damon Butler damon@hddesign.com
20 May 2003 14:53:38 -0500


I'd like to write a script that I can run from the command line -- NOT a
Python script object in Zope or even an external method -- that I can
run to upload files into Zope. Here's some code that I'm using right now
that, according to the documentation and examples I've seen for the
urllib2 library, OUGHT to work:

--------------------
import urllib2

pwdmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwdmgr.add_password(None,
	'http://localhost:8080/",
	'myusername',
	'mypassword')
auth = urllib2.HTTPBasicAuthHandler(pwdmgr)
opener = urllib2.build_opener(auth)
urllib2.install_opener(opener)

req = urllib2.Request('http://localhost:8080/manage_main')

f = urllib2.urlopen(req)
print f.read()
f.close()
--------------------

The line with the urlopen command always raises an error, however:

urllib2.HTTPError: HTTP Error 401: Unauthorized

The same script has no trouble obtaining the results of queries to
unprotected sites, but seems completely incapable of getting past the
Zope authorization. FYI: Tests with the wget utility show that it is
possible to get past the authorization, so the problem lies somewhere
with this script. What is it that I'm missing?

--Damon