Differences between revisions 3 and 6 (spanning 3 versions)
Revision 3 as of 2006-01-15 18:11:18
Size: 1165
Editor: NLV-Webproxy10
Comment:
Revision 6 as of 2006-12-08 11:24:04
Size: 1147
Editor: PaulBoddie
Comment: Removed "describe" message, formatted code.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
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.

'''The Following Does Not Work'''
How to store the variables, "HSstatsValue[i]" below?? The syntax is wrong, what is the actual syntax to store the variable or value???
Line 8: Line 4:
import urllib2, base64             for i in range(countTill):
                #print "cntTrack=", cntTrack
                s1 = re.compile(r"\d+").findall(lines[cntTrack+i+1])
                #print "s1=", s1
                #print "lines[cntTrack+i+1]=", cntTrack+i+1
                #oPutFile.write(stats[i])
                #print stats[i],
                #oPutFile.write('\t')
                if len(s1) == 1:
                    #print s1[0]+ '\n'
                    HSstatsValue[i]=s1[0]
                    #print "HSstatsValue[i]=", HSstatsValue[i]
                    oPutFile.write(s1[0])
                    oPutFile.write('\t')
                elif len(s1) > 1:
                    for x in range(len(s1)):
                        zum = zum + int(s1[x])
                        HSstatsValue[i]=s1[0]
Line 10: Line 23:
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,
                    oPutFile.write(str(zum))
                    #print zum
                    #oPutFile.write('\t')
                    oPutFile.write('\t')
                zum = 0
Line 36: Line 29:

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.

How to store the variables, "HSstatsValue[i]" below?? The syntax is wrong, what is the actual syntax to store the variable or value???

            for i in range(countTill):
                #print "cntTrack=", cntTrack
                s1 = re.compile(r"\d+").findall(lines[cntTrack+i+1])
                #print "s1=", s1
                #print "lines[cntTrack+i+1]=", cntTrack+i+1
                #oPutFile.write(stats[i])
                #print stats[i],
                #oPutFile.write('\t')
                if len(s1) == 1:
                    #print s1[0]+ '\n'
                    HSstatsValue[i]=s1[0]
                    #print "HSstatsValue[i]=", HSstatsValue[i]
                    oPutFile.write(s1[0])
                    oPutFile.write('\t')
                elif len(s1) > 1:
                    for x in range(len(s1)):
                        zum = zum + int(s1[x])
                        HSstatsValue[i]=s1[0]

                    oPutFile.write(str(zum))
                    #print zum
                    #oPutFile.write('\t')
                    oPutFile.write('\t')
                zum = 0

How to.../AccessTomcatFromPython (last edited 2008-11-15 14:00:38 by localhost)

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