Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old September 5th, 2003, 02:39 PM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 5
linking to functions in cgi?

what's up guys... i'm still sort of new to Python, and right now i have an assignment to do cgi... naturally, i decided python was my choice language.

is there a way that i can make a link that will invoke a function that is contained in the .py file? if so, is it also possible to pass parameters to the function call as well?

right now i have a lot of data that i need to present in a table, and i figured that it'd be best to separate it into different pages. so, i'm planning on having numbers corresponding to pages at the bottom, and i want to link it to a function call to calculate which data entries to display into the table template. thanks a bundle.

Reply With Quote
  #2  
Old September 5th, 2003, 05:52 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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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
Hey cvchenb, Python of course .. getting different results depending on what link is pressed really isn't too hard (providing your page and functions are set up correctly).

I'd suggest using url encoding.. I'm sure you've seen this before.

www.somesite.com/index.php?name=value&name=value

After the file extension (.php in this case) you have a question mark (?) followed by name=value pairs seperated by and-signs (&) simple stuff right, and passing data to a Python page like this is even easier, from a link all you need to do is something like this..

Code:
<a href="mycgi.py?show=1">page 1</a>

The CGI module provides a nice method of getting these values , you can then pass these values to your function to deside what data to display.

I hope you're with me here dude it isnt the easist subject explain well. Also I'd also check out http://www.devshed.com/Server_Side/.../CGI/page1.html for a short into to CGI programming with Python.

Hope this helps,

Have fun,
Mark.

Reply With Quote
  #3  
Old September 6th, 2003, 02:09 AM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 5
netytan:

thanks man, i appreciate the help. question though:

<a href="mycgi.py?show=1">page 1</a>

for 'mycgi.py?show=1' .. is 'show' the function while 1 is a parameter for show, or how does that work?

thanks again.

Reply With Quote
  #4  
Old September 6th, 2003, 06:49 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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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
very welcome Cv,

Not quite mate, show is the name of the variable and has a value of 1 i.e in Python, show = 1

Just incase here's a very basic CGI example for you , assuming that you have 'show=1' in your pathstring like in the previous example then this page should contain 'show = 1'..

Code:
#!/usr/bin/env python

import cgi

form = cgi.FieldStorage()

def value(key):
	print '%s = %s' % (key, form[key])

if __name__ == 'main':
	
	print 'Content-type: text/html\n'
	value('show')


Note: if you dont have show in your path string you'll get a key error. I find it helpful to think of it as if these 'name=value' pairs are a dictionary i.e {'show': 1}

Edit: I didn't actually test the CGI example above - just typed it strait into the browser - and i knowticed that i had passed a variable to value instead of a string. Changed that and it should work nicly now.

Take care,
Mark.

Last edited by netytan : September 7th, 2003 at 05:59 AM.

Reply With Quote
  #5  
Old September 6th, 2003, 01:43 PM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 5
OH ok... so... would i be incorrect in saying that it's like doing cgi through a form?

well, except passing the key/value pair through the link string is more explicit in "telling" the script what to do, right?

also, i was wondering... and i'm sorta doubtful that the answer will be yes, but i just want to know... is there a way to run and test cgi scripts written in python without access to a server (ie IIS or Apache?). at work they're still setting up my accounts and all that, i think it will be a while until i get access to a server, and they won't let me run my own apache because of security reasons. thanks Mark you're totally awesome

Reply With Quote
  #6  
Old September 7th, 2003, 07: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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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
Yeah, you got it now, like doing CGI froma form sept without the form ..

Anyway the answer is yes dude, although I cant say it's too easy.. you could write a small server in Python which can handle CGI scripts or use an existing one like medusa (http://www.nightmare.com/medusa/) which is the server under Zope!

If you look at the Python docs there are allot of great modules for this kind of thing i.e. BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer etc.

I've messed around with this in the past and it's pretty effective, I never got around to CGI from my lil server but the HTTP bit was cool.

My sugestion would have to be to do a search on google for a prebuilt Python cgi server, that is if madusa doesn't do it for you , i've heard some very good things!

Mark.

Reply With Quote
  #7  
Old September 17th, 2003, 11:18 AM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 5
hmm... any other way?

Mark,

Your suggestion was excellent but is there another way to do it? i have some global variables that are pretty important for my script to work, but passing keys and values by the way of

script.py?key=value

makes a new call to the script each time, and then i lose the values that are set into my global variables.

i think i've seen somewhere that someone used something like

script.py/email

to call an email() function that was defined inside of a file call script.py... but i haven't been able to get that to work for me =/

any more suggestions? i appreciate any and all help very very much.

Reply With Quote
  #8  
Old September 17th, 2003, 12:08 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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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
You might like to a look at Twisted Matrix as another Python server but i havn't used it and to be honest i don't know too much about it!

I've never seen this working before and i couldn't get it to work on Apache.. so no idea about that mate.. if you work it out do share i'd be very interested.

When your working on the net you need to put your data somewhere - a flat file, a DBM/pickle or a DB like MySQL or SQLite - if your want it to be available latter otherwise it just drop's of the end of the world .. It's just a web thing!

If the global variables your talking about are surposed to be the same every time just write them into your script with there values already set or even set a default if no other's are available?

I don't really know enough about your program to tell you the what, when, how and where's of storing your stuff

Mark.

Last edited by netytan : September 17th, 2003 at 12:31 PM.

Reply With Quote
  #9  
Old September 17th, 2003, 02:02 PM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 5
it's all good

Thanks, in any case... you're always very willing to help and that's really cool. I think I'm just gonna try to learn how to do cookies, so I can store the global variables that I need on the user's computer, and change them as necessary.

Reply With Quote
  #10  
Old September 17th, 2003, 03: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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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
Thanks allot cvchen!

Anyway cookies are a nice idea, if you need any help i'd be happy too lend a hand (or try)..

I think they thing which puts me off cookies is that some browsers just dont like them, and users can just turn them off. Which in some cases can be good, but it makes life harder

That being said there still pretty cool. You should check out the Cookie module:

http://www.python.org/doc/current/l...ule-Cookie.html

Alternativly you can set a cookie by sending the Set-Cookie header to the browser with the appropriate vaules, pretty simple

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > linking to functions in cgi?


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 |