Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 13th, 2004, 05:49 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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()

Reply With Quote
  #2  
Old February 13th, 2004, 06:06 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old February 13th, 2004, 06:10 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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

Reply With Quote
  #4  
Old February 13th, 2004, 06:10 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,381 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 5 m 43 sec
Reputation Power: 4080
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

Reply With Quote
  #5  
Old February 13th, 2004, 06:37 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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'

Reply With Quote
  #6  
Old February 14th, 2004, 07:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old February 15th, 2004, 07:10 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old February 15th, 2004, 07:17 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old February 16th, 2004, 03:40 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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>

Reply With Quote
  #10  
Old February 16th, 2004, 06:02 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #11  
Old February 16th, 2004, 07:09 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #12  
Old February 17th, 2004, 05:06 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Basic CGI

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap