|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
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. |
|
#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 |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
|||
|
|||
|
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.
|
|
#10
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > linking to functions in cgi? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|