I have been going through the lists and see lots of references to this
problem but I cannot see a solution that works, or should I say a
solution that I understand.
Currently I am accessing an eXist database through the sample Python
code, I need to use the Web API.
from string import rfind
collection =3D sys.argv
file =3D sys.argv
f =3D open(file, 'r')
print "reading file %s ..." % file
xml =3D f.read()
p =3D rfind(file, '/')
if p > -1:
doc =3D file[p+1:]
doc =3D file
print "storing document to collection %s ..." % collection
con =3D httplib.HTTP('localhost:8088')
con.putrequest('PUT', '/%s/%s' % (collection, doc))
clen =3D len(xml)
errcode, errmsg, headers =3D con.getreply()
if errcode !=3D 200:
f =3D con.getfile()
print 'An error occurred: %s' % errmsg
This works fine and does exactly wha tI want in terms of loading the
specified file. The problem as I am sure you all know too well is that
the connection defaults to the user guest. I need to load the data to a
nother user that has no pasword!
I am not a Python guru (my perl is only a lttle better) and indeed only
using python because it nearly fits my requirements. If someone could
provide some guidance it would be very much appreciated.
On 22 Mar 2006 at 16:55, Simon Tuson wrote:
> import httplib
I posted an example on the wiki that uses urllib2 to inject Basic Authentication.
> con = httplib.HTTP('localhost:8088')
> con.putrequest('PUT', '/%s/%s' % (collection, doc))
> con.putheader('Content-Type', 'text/xml')
you need to add the Authorization header..
s = userid + ":" + password
auth_string = base64.encodestring(s)[:-1] # remove trailing \n
con.putheader('Authorization': 'Basic %s' % auth_string)
Though see the wiki post above for a general purpose class that exposes both
REST and xml-rpc interfaces and has usage example.
Brad Clements, bkc@... (315)268-1000
AOL-IM or SKYPE: BKClements
basically you have to send these authorization headers together with
your first call. ("Pre emptive"). Exist will give you the default
credentials (guest) of you don't provide any details. A '401' is never