Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
python with tomcat on windows

Posted by tasslehoff (tasslehoff), 9 October 2007
I'm trying to run python/cgi scripts on tomcat, but i'm running into quite some trouble.

I have tomcat and python installed, and have managed to run scripts using perl, but when i switched to python, nothing will run - i just get a blank screen. I already switched the cgi executable for python, and it's detected, so i can't imagine why it will only give me a blank screen, no errors or exceptions show.

If any of you has an idea of what may be happening, i greatly appreciate your help!

Posted by admin (Graham Ellis), 9 October 2007
Have you

1. Rewritten the Perl code into Python?

2. Changes the #! line to point at Python not Perl?

3. Ensured that your Python program outputs the appropriate headers?

4. Tested your Python from the command line.

It's quite hard to sort out the more generic "does not work" type questions ... especially without seeing any of the code we're looking for problems in    ... can I suggest that you post a very simple Python script (of the "hello world" flavour) that fails for you, and I can se eif I can spot any issues and / or test it on my own system.

Posted by tasslehoff (tasslehoff), 9 October 2007
Well, i managed to run a Hello World, but the script i need to run still won't work - i get a http 500 error. It's a simple proxy script from, and it should work *sigh*. I'll post the code anyway

#!c:/Python/python.exe -u

"""This is a blind proxy that we use to get around browser
restrictions that prevent the Javascript from loading pages not on the
same server as the Javascript.  This has several problems: it's less
efficient, it might break some sites, and it's a security risk because
people can use this proxy to browse the web and possibly do bad stuff
with it.  It only loads pages via http and https, but it can load any
content type. It supports GET and POST requests."""

import urllib2
import cgi
import sys, os

# Designed to prevent Open Proxy type stuff.

allowedHosts = ['', '',
               '', '',
               '', '']

method = os.environ["REQUEST_METHOD"]

if method == "POST":
   qs = os.environ["QUERY_STRING"]
   d = cgi.parse_qs(qs)
   if d.has_key("url"):
       url = d["url"][0]
       url = ""
   fs = cgi.FieldStorage()
   url = fs.getvalue('url', "")

   host = url.split("/")[2]
   if allowedHosts and not host in allowedHosts:
       print "Status: 502 Bad Gateway"
       print "Content-Type: text/plain"
       print "This proxy does not allow you to access that location."
       print os.environ
   elif url.startswith("http://") or url.startswith("https://"):
       if method == "POST":
           length = int(os.environ["CONTENT_LENGTH"])
           headers = {"Content-Type": os.environ["CONTENT_TYPE"]}
           body =
           r = urllib2.Request(url, body, headers)
           y = urllib2.urlopen(r)
           y = urllib2.urlopen(url)
       # print content type header
       i =
       if i.has_key("Content-Type"):
           print "Content-Type: %s" % (i["Content-Type"])
           print "Content-Type: text/plain"
       print "Content-Type: text/plain"
       print "Illegal request."

except Exception, E:
   print "Status: 500 Unexpected Error"
   print "Content-Type: text/plain"
   print "Some unexpected error occurred. Error text was:", E

Posted by admin (Graham Ellis), 10 October 2007
If "hello world" works but your longer bit of code doesn't. thn you should be able to start adding bits in to "Hello world" until it fails, or taking bits out of your other code until it works, to give you a pointer as to where the problem area lies.   Once you have the problem area isolated down to 3 or 4 lines, it should be possible to spot the problem or do further experiments to find what is wrong in that area.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho