473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A couple of Python CGI questions

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
4 1609
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
4
by: Skip Montanaro | last post by:
(moving over from webmaster mailbox) scott> I'm sorry for bothering you, but I've tried to post to the Python scott> Tutor Mail List, tried to get someone from Bay PIggies to scott> respond, but...
0
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
9
by: dasacc | last post by:
(1) How do I perform a search for "word" and have it return every line that this instance is found? (2) How do I perform a search for "word" and "wordtwo" at the same time to return every line...
3
by: digitalsubjunctive | last post by:
Hey, I just started on Python and have a few questions I couldn't find answers to on the Python site or it's tutorial. 1. I notice a few "compiled python" files (indicated by reddish snake...
12
by: John Salerno | last post by:
I've been looking around and reading, and I have a few more questions about SQLite in particular, as it relates to Python. 1. What is the current module to use for sqlite? sqlite3? or is that not...
4
by: Emanuele D'Arrigo | last post by:
Hi everybody, I'm just having a go with Unit Testing for the first time and my feeling about it in short is: Neat! I'm a bit worried about the time it's taking me to develop the tests but...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.