The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Python CGI
Discuss Python CGI in the Python Programming forum on Dev Shed. Python 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 18th, 2004, 09:33 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Canada
|
|
|
Python CGI
I'm new to using Python on the internet. To make HTML files, using Python stuff in the files, etc., etc. So please excuse any mistakes I use in my wording. A simple correction will do.
http://24.125.59.73/tests/testfile.py
Is it possible to have a form, and what you put in the text area, will be the raw_input. You then tell Python what to do with it, and display it on a page.
If it is possible, can someone show me how I can do it?
Lets use the following example (found here):
Code:
print raw_input('Enter the word to be Reversed: ')[::-1]
Netytan deserves credit for the code above. Thanks!
|

February 19th, 2004, 12:08 AM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
First you'll need a few lines of Javascript I believe, to handle fields and buttons. I leave that to you since my Javascript skills are t3h sucks.
That being said, what you need is a base URL (like http://www.url.com/script.cgi) to which you'll pass arguments. Simply use a syntax like http://www.url.com/script.cgi?arg=blabla , where blabla is what the user entered in the field.
Now Python comes into play. Use Netytan's code with this being put over it :
Code:
sys.stderr = sys.stdout
form = cgi.FieldStorage()
if form.has_key("arg"):
the_arg = form.getvalue("arg")
#put the rest of the code here
else:
#put a nice error message for your user
And that's it. Don't forget to import cgi and sys ! 
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz
|

February 19th, 2004, 05:39 AM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Canada
|
|
Yeah, my Javascript skills aren't so great. I hate the language, and have never needed it.
The program you are helping me make, can be found here:
http://24.125.59.73/tests/testfile2.py
Source code:
Code:
#!/usr/bin/env python
print 'Content-Type: text/html\n'
print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
print '<html lang="en">'
print ' <head>'
print ' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
print ' <title>Python: Test Page 2</title>'
print ' </head>'
print ''
print ' <body>'
import cgi
import sys
sys.stderr = sys.stdout
form = cgi.FieldStorage()
if form.has_key("arg"):
the_arg = form.getvalue("arg")
print raw_input("Enter the word to be reversed: ")[::-1]
else:
print "The program did not execute correctly."
print '<p> <a href="http://24.125.59.73/tests/testfile2.py?arg=test" title="?arg=test">?arg=test</a> </p>'
print ' </body>'
print '</html>'
If you know Javascript, we'd really appreciate it if you helped us out with that part. Thanks!
Last edited by MasterChief : February 19th, 2004 at 05:51 AM.
|

February 19th, 2004, 11:48 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
As I said, I really suck at Javascript - and BTW I absolutely can't access your HTTP server, sorry. But here's what a page MIGHT look like. With no Javascript involved
Inside your <body> tags, add forms this way :
Code:
<form name="formname" action="text.py" method="post">
<input type="text" size="20" name="inputname">
<input type="submit" name="B1" value="Submit">
</form>
Not sure if the inputs' types need quotes, so try unquoting if it's not working. With this, your browser will take care of all syntax matters as to how to access your script ; if it doesn't work, try renaming it to .cgi.
It's really late, I just hope I'm making sense.
|

February 20th, 2004, 05:55 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Quote: | Originally Posted by SolarBear Now Python comes into play. Use Netytan's code with this being put over it :
Code:
sys.stderr = sys.stdout
form = cgi.FieldStorage()
if form.has_key("arg"):
the_arg = form.getvalue("arg")
#put the rest of the code here
else:
#put a nice error message for your user
|
If you have Python 2.3 you can also check is the key exists in a dictionary using this (much) cleaner method. although for now the number of servers running Python 2.3 is kinda low, but for a localhost...
Code:
if 'arg' in form:
the_arg = form.getvalue("arg")
else:
print 'Oops'
Mark.
__________________
programming language development: www.netytan.com – Hula
|

February 20th, 2004, 06:03 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Canada
|
|
http://24.125.59.73/tests/testfile2.py
Here is an exact copy of my source code:
testfile2.py
Code:
#!/usr/bin/env python
print 'Content-Type: text/html\n'
print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
print '<html lang="en">'
print ' <head>'
print ' <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
print ' <title>Python: Test Page 2</title>'
print ' </head>'
print ''
print ' <body>'
import cgi
import sys
sys.stderr = sys.stdout
form = cgi.FieldStorage()
if 'arg' in form:
the_arg = form.getvalue("arg")
print raw_input("Enter the word to be reversed: ")[::-1]
else:
print "The program did not execute correctly."
print '<p> <a href="http://24.125.59.73/tests/testfile2.py?arg=test" title="?arg=test">?arg=test</a> </p>'
print ' <form name="formname" action="testfile2.py" method="post">'
print ' <input type="text" size="20" name="inputname">'
print ' <input type="submit" name="B1" value="Submit">'
print ' </form>'
print ' </body>'
print '</html>'
What now?
(Thanks guys, for all of the help so far.)
Last edited by MasterChief : February 20th, 2004 at 06:14 PM.
|

February 21st, 2004, 05:32 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Hi Chief,
you should note that using raw_input() in a webpage is a bad idea, often leading to an error... that is unless you have submitted a form using POST methods to that page in which case it displays a --------number string, nothing more  .
Why are you using the getvalue('key') method to access the value in you're form, since we've already desided that the form value exists (in the if statment) and you're not setting a default value anyway  . Why not use this form.
Code:
>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> d['a']
1
>>> d['b']
2
>>>
This is just a dictionary, in FieldStorage() you need to append .value to then end of you're dictionary what variable you want i.e.
Code:
>>> d['c'].value #Note don't try this with a normal dictionary :) or you'll get a nice error message; but works with FieldStorage()
3
Catch you latter guys.
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
|
|
|
|
|