Thread: Basic CGI

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep 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()
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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
    programming language development: www.netytan.com Hula

  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep 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'
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep 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.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep 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>
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep 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.
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

Similar Threads

  1. refferencing a cgi to a form embedded within the cgi?
    By nickname in forum Python Programming
    Replies: 0
    Last Post: May 15th, 2002, 07:48 AM
  2. CGI with C++???
    By Josh0r in forum Beginner Programming
    Replies: 1
    Last Post: October 30th, 2001, 11:19 AM
  3. CGI communication to Non CGI
    By Tool God in forum Perl Programming
    Replies: 2
    Last Post: October 3rd, 2001, 04:27 AM
  4. Calling php script from perl cgi script
    By cschilbe in forum Perl Programming
    Replies: 1
    Last Post: May 28th, 2001, 05:52 PM
  5. CGI Book?
    By Parallax in forum Perl Programming
    Replies: 3
    Last Post: September 14th, 2000, 05:09 PM

IMN logo majestic logo threadwatch logo seochat tools logo