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

Simon Willison cs1spw at bath.ac.uk
Mon Nov 26 11:03:02 CET 2001

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
<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': '', 'map': 'q3dm8'}, {'maxplayers': '12',
'name': 'b
lueyonder Quake 3 Free For All #1', 'players': '0', 'ip':
'map': 'q3dm13'}]

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.



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
	file = open(tmp, 'r')
	lines = file.readlines()
	for line in lines:
		if (string.strip(line)):
				bits = string.split(line, '~@~@~')
			except ValueError:
			if (string.strip(bits[2]) in badstatus):
				# 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
	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()
	for line in lines:
		line = string.replace(line, '%title%', title)
		print line
	print html
	file = open('footer.txt', 'r')
	lines = file.readlines()
	for line in lines:
		print line

def custom_die(message):
	""" Die with an error message """
	output('Error!', message)

More information about the Python-list mailing list