I have the following simple Python code that makes a simple post request to a REST service -
params= { "param1" : param1,
"param2" : param2,
"param3" : param3 }
xmlResults = urllib.urlopen(MY_APP_PATH, urllib.urlencode(params)).read()
results = MyResponseParser.parse(xmlResults)
The problem is that the url used to call the REST service will now require basic authentication (username and password). How can I incorporate a username and password / basic authentication into this code, as simply as possible?
You may want a library to abstract out some of the details. I've used restkit to great effect before. It handles HTTP auth.
If basic authentication = HTTP authentication, use this:
If not, use
mechanize
orcookielib
to make an additional request for logging in. But if the service you access has an XML API, this API surely includes auth too.2016 edit: By all means, use the requests library! It provides all of the above in a single call.