The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Basic CGI
Discuss Basic CGI in the Python Programming forum on Dev Shed. Basic CGI Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 13th, 2004, 05:49 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Basic CGI
This code is from a tutorial, but it doesn't seem to work and I have no idea why. I get the typical "Internal Server Error". It's weird because It works fine if I remove the functions. I can simply print "Hello, World" and not call any function but I still get that error  .
Code:
#!/usr/bin/env python
import cgi
import re
def displayContent(content):
template = open('template.html', 'r')
input = template.read()
template.close()
subResult = re.subn('<!-- Content Here -->', content, input)
if subResult[1] = 0:
raise 'Error'
print "Content-Type: text/html\n\n"
print subResult[0]
def displayForm():
formHandle = open('form.html', 'r')
formInput = formHandle.read()
formHandle.close()
displayContent(formInput)
def processForm(form):
output = ''
subject = form['subject'].value
author = form['author'].value
body = form['body'].value
output = subject + ' ' + author + ' ' + body
displayContent(output)
form = cgi.FieldStorage()
try:
key = forum['process'].value
except:
key = None
if key == 'process':
parseForm(form)
else:
displayForm()
|

February 13th, 2004, 06:06 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Just a guess but could it have something to do with there not being a function called parseForm() anywhere i you're code... If you remove all the functions then you'll get an error because theres no Content-Type header i.e.
Code:
#!/usr/bin/env python
print 'Content-Type: text/plain\n'
print 'Hello World From Xx'
Thats about as basic as you're gonna get. If you want to redirect errors to the browser so you dont have to keep opening you're apache error log (which can be very anoying) just add these line to your CGI file  .
Code:
#!/usr/bin/env python
import sys
sys.stderr = sys.stdout
print 'Content-Type: text/plain\n'
print 'Hello World From Xx'
You should also check out the cgitb module although personally i puke every time i see that neon screen!
http://www.python.org/doc/2.3.3/lib/module-cgitb.html
You might also wana check that you're Python file is pointing at the Python interpreter. A very comon mistake by both new and old CGI users
Anyway give that a go  and let me know what happens.
Mark.
__________________
programming language development: www.netytan.com – Hula
|

February 13th, 2004, 06:10 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Hi Scorp  , the Content-Type header is been sent as part of the displayContent() function... Not the most clear design  , especially since it came from a CGI tutorial!
Catch ya latter,
Mark
|

February 13th, 2004, 06:10 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Also might help if you have access to your error log. You should check to see if there are any helpful messages there.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

February 13th, 2004, 06:37 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I made some changes but it still doesn't work.
@netytan: I added the sys.stderr = sys.stdout but it doesn't show anything in the browser
Code:
#!/usr/bin/env python
import cgi
import re
import sys
def displayContent(content):
try:
template = open('template.html', 'r')
except IOError:
print 'IO Error'
input = template.read()
template.close()
subResult = re.subn('<!-- Content Here -->', content, input)
if subResult[1] = 0:
raise 'Error'
print "Content-Type: text/html\n\n"
print subResult[0]
def parseForm(form):
output = ''
subject = form['subject']
author = form['author']
body = form['body']
output = subject + ' ' + author + ' ' + body
displayContent(output)
sys.stderr = sys.stdout
form = cgi.FieldStorage()
parseForm(form)
#print "Content-Type: text/html\n\n"
#print 'Hi'
|

February 14th, 2004, 07:13 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Sorry for the slow responce Xx, busy, busy day! Anyway the reason you seem to be suffering here is because the page flow is all messed up i.e. the try-except block would cause an error is template doesn't exist since the program keeps on running regardless of an IOError or not. Oh and a minor syntax error/typo (= in stead of ==).
Code:
#!/usr/bin/env python
import cgi, re, sys
sys.stderr = sys.stdout
def displayContent(content):
try:
template = file('template.html', 'r')
input = template.read()
template.close()
subResult = re.subn('<!-- Content Here -->', content, input)
if subResult[1] == 0:
raise 'Error'
print subResult[0]
except IOError:
print 'IO Error'
def parseForm(form):
subject = form['subject']
author = form['author']
body = form['body']
output = subject, author, body
displayContent(output)
if __name__ == '__main__':
print "Content-Type: text/html\n"
form = cgi.FieldStorage()
parseForm(form)
Anyway i've tidied it up a little. Hopefully it'll behave for you now! This is untested btw since i dont know what this is surposed to do or what template.html is surposed to look for and etc.
Note: sys.stderr = sys.stdout should be writen as soon as it can be since it'll only redirect errors AFTER it's called.
Mark.
|

February 15th, 2004, 07:10 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks netytan! I'm busy too (with school). Thanks for the new code, I'll test it when I can and hopefuly I can get it working. 
|

February 15th, 2004, 07:17 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
No problem Xx. If it doesn't work/you can get it working just let me know and i'll see what i can do to help!
Mark.
|

February 16th, 2004, 03:40 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I just tryed the new code and I get this error:
Code:
Traceback (most recent call last): File "/Library/WebServer/CGI-Executables/formParse.py", line 76, in ? parseForm(form) File "/Library/WebServer/CGI-Executables/formParse.py", line 60, in parseForm displayContent(output) File "/Library/WebServer/CGI-Executables/formParse.py", line 27, in displayContent subResult = re.subn('', content, input) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre.py", line 151, in subn return _compile(pattern, 0).subn(repl, string, count) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre.py", line 257, in _subx template = _compile_repl(template, pattern) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre.py", line 242, in _compile_repl p = sre_parse.parse_template(repl, pattern) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre_parse.py", line 645, in parse_template s = Tokenizer(source) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre_parse.py", line 186, in __init__ self.__next() File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/sre_parse.py", line 192, in __next if char[0] == "\\": AttributeError: MiniFieldStorage instance has no attribute '__getitem__'
...and I'm really not sure what it means
btw here is form.html and template.html if anyone wants it
form.html:
Code:
<html>
<head>
<title>bleh</title>
</head>
<body>
<font size=3><b>Test Form</b>
<font face=verdana size=2>
<form action="cgi-bin/formParse.py" method="post">
<p>
Subject: <br />
<input type="text" size="50" name="subject"><br>
Author: <br />
<input type="text" size="50" name="author"><br>
<br />
Body:<br />
<textarea name="body" rows="12" cols="46"></textarea>
</p>
<input type="Submit" value="Submit">
</form>
</body>
</html>
template.html:
Code:
<html>
<head>
<title>Template</title>
</head>
<body>
<!-- Content Here -->
</body>
</html>
|

February 16th, 2004, 06:02 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Ok, the problem was being cause by the content variable; basically, re.sub() was barfing because it was being passed instances instead of a string (in content).
Anyway it wasn't hard to fix when i got down to it, all i've really done here is assign the form[key].value string to content instead of the form[key] instance!
Code:
import cgi, re, sys
sys.stderr = sys.stdout
def displayContent(content):
try:
template = file('template.html', 'r').read()
result = re.sub('<!-- Content Here -->', content, template)
print result
except IOError:
print 'IO Error'
def parseForm(form):
subject = form['subject'].value
author = form['author'].value
body = form['body'].value
output = subject, author, body
displayContent(output)
if __name__ == '__main__':
print "Content-Type: text/html\n"
form = cgi.FieldStorage()
parseForm(form)
Seems like a pretty useless example though
Mark.
Last edited by netytan : February 16th, 2004 at 06:05 PM.
|

February 16th, 2004, 07:09 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Ahhhh it works  , thanks Mark! This is all for a news management system I want to write to use on my websites, although, I'm not sure if using this method is the best way to do it.
|

February 17th, 2004, 05:06 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Ah i see  . personally i dout i'd use this myself, just too many holes i.e. if say, the subject wasn't given you'll have a lovly error message. Also there isn't much point in using regex for somthing this simple, i'd consider using the replace() method instead and removing regex all together.
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|