import of 'string' fails in CGI-BIN script...but works in IDLE

John F Dutcher John_Dutcher at urmc.rochester.edu
Tue Dec 2 13:40:04 EST 2003


Having 'cloned' an existing python script that imports 'string' and
uses "string.rstrip()" without incident...
I am at a loss to explain why the new 'cloned' script (brief sample
below) continually errors with:
 NameError: global name 'string' is not defined 
 args = ("global name 'string' is not defined",) 

when run from the server CGI-BIN. 

If the same script is run in 'IDLE' it
works with no problem. Yet, the "import urllib, string" statement is
clearly
present in the script.

I am running Python 2.3.1 on Win 95, using XITAMI as the server.

Troublesome script follows:

#!c:\Python\python.exe
import cgitb; cgitb.enable()
import traceback, sys, os, cgi
import urllib, string   
sys.stderr = sys.stdout
form = cgi.FieldStorage()
def writeHTML(rec):
    print "Content-Type: text/html"
    print
    print '''    
<FORM ACTION="/cgi-bin/dietsys7.py" METHOD="POST">
<CENTER>
<B><INPUT TYPE="SUBMIT" VALUE="Click to Apply Changes"></B><br><br>'''
    print '''
<TABLE BORDER=1 BGCOLOR="FFFF99">'''
    cycle = 1
    drptr = 7
    for x in range(10):
        uvalue = string.rstrip(rec[drptr]) <-----(** failure occurs
here)
        if not ((cycle % 2) == 0): 
            print '''<TR bgcolor="pink"><td>'''
        else:
            print '''<TR bgcolor="yellow"><td>'''
        print '''<INPUT TYPE="text" NAME=''' + '"' + 'doctorcode' +
str(cycle) + '"' + ' ' + '''size="36" maxlength="1" value=''' + '"' +
uvalue + '"' + ' ' + '''>'''
        print '''<td><b>Doctor Color Code</B></td></tr>'''
        cycle = cycle + 1
        drptr = drptr + 1
print '''</table></FORM></FONT></BODY></HTML>'''   
fileName = os.path. join("c:", "/xitami", "cgi-bin", "dsysmst.dat")
#print 'Globals: ' + str(globals())
#print 'Locals: ' + str(locals())
f = open(fileName, 'rb')
f.seek(-2690,2)
recd = f.read(2690)
if recd[3:7] == '0000':
    f.close()
    writeHTML(recd)
    sys.exit()
else:
    print "Did not get utility record"
    f.close()
    sys.exit()




More information about the Python-list mailing list