By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

A couple of Python CGI questions

P: n/a
I tried getting mod_python to work but have had lots of problems getting it
running. So, I have settled on using python cgi.

I have already got it going and it seems to run fast enough for me.

My questions are:
1. Is there any way to access variables from within a string like perl cgi?
I have found that the best way for me to write html tables is to use a print
"""<blah blah blah>""" string. That way I can write one print """, and then
write html as if I were writing an html file. For example:
print """
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
"""
I really like this and it is a lot like typing the following in Perl cgi:
print <<EOF
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
EOF

So, is there any way to access variables from within the string, besides
using %s? In perl, I just put the $variable in the string and it replaces
the varname with the value.

2. Is there a good online resourse for pyton cgi?
3. How do you collect request variables from forms sent to the python cgi
page, whether post or get?
Thanks very much for the help... I will continue to google for my answers.

Sean

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I think I found the answer to my third question. I found an example using
something like the following.

form = cgi.FieldStorage()
" userid" = form['userid'].value

but should it be "userid" =?
I would think it should be userid =.

Thanks.
"sean" <se********@cox.net> wrote in message
news:9g8ac.65379$Bg.14309@fed1read03...
I tried getting mod_python to work but have had lots of problems getting it running. So, I have settled on using python cgi.

I have already got it going and it seems to run fast enough for me.

My questions are:
1. Is there any way to access variables from within a string like perl cgi? I have found that the best way for me to write html tables is to use a print """<blah blah blah>""" string. That way I can write one print """, and then write html as if I were writing an html file. For example:
print """
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
"""
I really like this and it is a lot like typing the following in Perl cgi:
print <<EOF
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
EOF

So, is there any way to access variables from within the string, besides
using %s? In perl, I just put the $variable in the string and it replaces
the varname with the value.

2. Is there a good online resourse for pyton cgi?
3. How do you collect request variables from forms sent to the python cgi
page, whether post or get?
Thanks very much for the help... I will continue to google for my answers.

Sean

Jul 18 '05 #2

P: n/a
What about collection variables passed via the URL... like
someprogram.cgi?userid=Seam

How would I collect the userid = Sean part?

Thanks in advance.

"sean" <se********@cox.net> wrote in message
news:9g8ac.65379$Bg.14309@fed1read03...
I tried getting mod_python to work but have had lots of problems getting it running. So, I have settled on using python cgi.

I have already got it going and it seems to run fast enough for me.

My questions are:
1. Is there any way to access variables from within a string like perl cgi? I have found that the best way for me to write html tables is to use a print """<blah blah blah>""" string. That way I can write one print """, and then write html as if I were writing an html file. For example:
print """
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
"""
I really like this and it is a lot like typing the following in Perl cgi:
print <<EOF
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
EOF

So, is there any way to access variables from within the string, besides
using %s? In perl, I just put the $variable in the string and it replaces
the varname with the value.

2. Is there a good online resourse for pyton cgi?
3. How do you collect request variables from forms sent to the python cgi
page, whether post or get?
Thanks very much for the help... I will continue to google for my answers.

Sean

Jul 18 '05 #3

P: n/a
Figured that one out too.

form = cgi.FireldStorage()
userid = form.getfirst("userid")

print userid

Does this work that same for forms? Since this is get, and most forms will
be post... can I collect post variables in the same fashion?
"sean" <se********@cox.net> wrote in message
news:2z8ac.65499$Bg.26470@fed1read03...
What about collection variables passed via the URL... like
someprogram.cgi?userid=Seam

How would I collect the userid = Sean part?

Thanks in advance.

"sean" <se********@cox.net> wrote in message
news:9g8ac.65379$Bg.14309@fed1read03...
I tried getting mod_python to work but have had lots of problems getting

it
running. So, I have settled on using python cgi.

I have already got it going and it seems to run fast enough for me.

My questions are:
1. Is there any way to access variables from within a string like perl

cgi?
I have found that the best way for me to write html tables is to use a

print
"""<blah blah blah>""" string. That way I can write one print """, and

then
write html as if I were writing an html file. For example:
print """
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
"""
I really like this and it is a lot like typing the following in Perl cgi: print <<EOF
<table border="0" cellspacing="0">
<tr>
<td>This is cool</td>
</tr>
</table>
EOF

So, is there any way to access variables from within the string, besides
using %s? In perl, I just put the $variable in the string and it replaces the varname with the value.

2. Is there a good online resourse for pyton cgi?
3. How do you collect request variables from forms sent to the python cgi page, whether post or get?
Thanks very much for the help... I will continue to google for my answers.
Sean


Jul 18 '05 #4

P: n/a
"sean" <se********@cox.net> wrote:
userid = form.getfirst("userid")
Yes, this the the most convenient way of using FieldStorage, in Python 2.2 upwards.
can I collect post variables in the same fashion?
Yes. In a POST request, name/value pairs are read from the body; any
pairs in a query string are ignored.
Is there any way to access variables from within a string like perl cgi?


Sort of. The basic substitution mechanism is:

'<el> Hello %s %s! </el>' % (cgi.escape(title), cgi.escape(person.name))

Or you can use named substitutions by using a dictionary:

'<el> Hello %(title)s %(name)s! </el>' % {
'title': cgi.escape(title), 'name ': cgi.escape(name)
}

And you can get a dictionary containing local variables using locals():

t= cgi.escape(title)
n= cgi.escape(name)
'<el> Hello %(t)s %(n)s! </el>' % locals()

This is as far as Python goes, but there are of course a million templating
languages that take things up from there and which are a sensible choice
for anything more complicated. [insert standard PXTL plug here. ed.]

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.