(newbie) A very weird Python CGI problem, possibly related to os.system

Steve Holden sholden at holdenweb.com
Mon Nov 26 07:37:23 EST 2001


"Simon Willison" <cs1spw at bath.ac.uk> wrote ...
> I have a very odd Python CGI problem. I'm writing a simple web script
> which queries Quake III game servers and displays the server name /
> map / number of players on a web page. The script works by executing a
> command line program called qstat (www.qstat.org). I'm very new to
> python, and I'm using this project as a learning excercise.
>
> Anyway, here's the problem: When I run the script I have written from
> the command line it works fine:
>
> bwaf $ qstat.py
> <html>
> <head>
> <title>OMG!!</title>
> </head>
> <body>
> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">
> <b>ResNet Game Server Listing System</b><p>
> [{'maxplayers': '12', 'name': 'blueyonder Quake 3 Free For All #2',
> 'players': '
> 0', 'ip': '62.30.30.160:27961', 'map': 'q3dm8'}, {'maxplayers': '12',
> 'name': 'b
> lueyonder Quake 3 Free For All #1', 'players': '0', 'ip':
> '62.30.30.160:27960',
> 'map': 'q3dm13'}]
> </font>
> </body>
> </html>
>
> However, when I visit the script on the web the page displayed shows
> an empty list:
>
> ResNet Game Server Listing System
>
> []
>
> The script is here: http://www.bath.ac.uk/~cs1spw/cgi/servers/qstat.py
>
> I'm completely baffled as to why the list is populated when I run the
> script myself, but appears empty when the script is executed via the
> web. Could this be a unix permissions problem? I doubt it, as a
> smaller test script which I wrote to display results from qstat shows
> information in the browser fine. Unfortunately the script is quite
> long so I'll only post a snippet here, I'm happy to post the rest (or
> place it on the web) if it will help solve the problem.
>
> Cheers,
>
> Simon
>
> import os, sys, string, tempfile, common
> from common import custom_die, servertypes
>
> qstat = '/u/cs/1/cs1spw/qstat24e/qstat' # Path to qstat
> tempdir = '/u/cs/1/cs1spw/public_html/cgi/servers' # Used by mktemp()
> badstatus = ['DOWN', 'TIMEOUT']
>
> def gameservers(game):
> """ returns array of dictionaries of server details for game """
> array = []
> # Run qstat on the game file
> if (not servertypes.has_key(game)):
> custom_die('Invalid game specified!')
> tmp = tempfile.mktemp()
> run = qstat + " -raw ~@~@~ -f " + game + " > " + tmp
> os.system(run)
> file = open(tmp, 'r')
> lines = file.readlines()
> file.close()
> os.remove(tmp)
> for line in lines:
> if (string.strip(line)):
> try:
> bits = string.split(line, '~@~@~')
> except ValueError:
> continue
> if (string.strip(bits[2]) in badstatus):
> continue
> else:
> # Create a dictionary of details about the server
> dict = {}
> dict = {
> 'ip': bits[1],
> 'name': bits[2],
> 'map': bits[3],
> 'maxplayers': bits[4],
> 'players': bits[5]
> }
> # put the dictionary in the array
> array.append(dict)
> return array
>
> # test code
> common.output('OMG!!', gameservers('q3s'))
>
> And from common.py...
>
> servertypes = {
> 'q3s': 'Quake III',
> 'hls': 'Half-Life'
> }
>
> def output(title, html):
> """ outputs a page using header.txt with %title% replaced and
> footer.txt """
> file = open('header.txt', 'r')
> lines = file.readlines()
> file.close()
> for line in lines:
> line = string.replace(line, '%title%', title)
> print line
> print html
> file = open('footer.txt', 'r')
> lines = file.readlines()
> file.close()
> for line in lines:
> print line
> sys.exit(0)
>
> def custom_die(message):
> """ Die with an error message """
> output('Error!', message)

What server are you running with? If it's Apache, you should look in the
error log to see whether messages are being logged. Perhaps environment
differences are causing errors you aren't seeing?

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list