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 18th, 2004, 09:33 PM
MasterChief MasterChief is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 543 MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 11 h 18 m 34 sec
Reputation Power: 23
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!

Reply With Quote
  #2  
Old February 19th, 2004, 12:08 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
Lightbulb

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

Reply With Quote
  #3  
Old February 19th, 2004, 05:39 AM
MasterChief MasterChief is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 543 MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 11 h 18 m 34 sec
Reputation Power: 23
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.

Reply With Quote
  #4  
Old February 19th, 2004, 11:48 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
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.

Reply With Quote
  #5  
Old February 20th, 2004, 05:55 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
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


Reply With Quote
  #6  
Old February 20th, 2004, 06:03 PM
MasterChief MasterChief is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 543 MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 11 h 18 m 34 sec
Reputation Power: 23
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.

Reply With Quote
  #7  
Old February 21st, 2004, 05:32 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
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python 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