Revision 2 as of 2006-01-15 18:03:36

Clear message

Looking for examples of accessing Tomcat applications from Python.

I will be looking myself, and will post what I learn here, but so far Tomcat applications that require authentication are giving my python applications a 401 no matter what I try.

Does Not Work

import urllib2, base64

myUrl = 'http://localhost:8080/manager/html'
username = 'root'
password = 'r00t4me'

req = urllib2.Request(myUrl)

base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader =  "Basic %s" % base64string
req.add_header("Authorization", authheader)

try:
    f = urllib2.urlopen( myUrl )
except urllib2.HTTPError, e:
    if e.code == 401:
        print 'not authorized'
    elif e.code == 404:
        print 'not found'
    elif e.code == 503:
        print 'service unavailable'
    else:
        print 'unknown error: '
else:
    print 'success'
    for line in f:
        print line,

This yields a 401 'not authorized' when it should not, i.e. the username and password are correct. This is to connect to the page for the Tomcat manager application as a test, not to do any real work.

Unable to edit the page? See the FrontPage for instructions.