473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

CGI module does not parse data

I am writing a webserver, and I want it to be able to run python
scripts. But when I set sys.stdin to self.rfile (using the
BaseHTTPServer class, self.rfile is a input stream containing the
request), the cgi module does not parse the data.
Example script:
import cgi
form = cgi.FieldStorage()
print form["testfield"]

The above script would print nothing. Should i be setting sys.stdin to
something else, or is there anthoer solution entirely?

Dec 1 '05 #1
12 1581
"amfr" wrot3e:
I am writing a webserver, and I want it to be able to run python
scripts. But when I set sys.stdin to self.rfile (using the
BaseHTTPServer class, self.rfile is a input stream containing the
request), the cgi module does not parse the data.
Example script:
import cgi
form = cgi.FieldStorage()
print form["testfield"]

The above script would print nothing. Should i be setting sys.stdin to
something else, or is there anthoer solution entirely?


are you writing your own CGIHTTPServer ?

http://docs.python.org/lib/module-CGIHTTPServer.html

why ?

</F>

Dec 1 '05 #2
I have included some of the content of that file, I am writing this as
an extension to my ebserver which is based on BaseHTTPServer. This
part of the code was taken directly from the CGIHTTPServer file,
nothing changed

Dec 1 '05 #3
I just read somewhere that the CGIHTTPServer module does not work on
mac (which I am currently using), is this true?

Dec 1 '05 #4
amfr wrote:
I just read somewhere that the CGIHTTPServer module does not work on
mac (which I am currently using), is this true?


It might help a lot if you could include a link to "somewhere", so we'd
know what "does not work" meant... often it means one particular feature
is not perfect, as opposed to "crashes and burns" or "does nothing at
all"...

Also, someone who has it "working" on a Mac hasn't necessarily tried out
the entire range of functionality so the thing you read might still be
correct.

-Peter

Dec 2 '05 #5
Le die Thu, 01 Dec 2005 15:08:14 -0800, amfr ha scribite:
I have included some of the content of that file, I am writing this as
an extension to my ebserver which is based on BaseHTTPServer. This
part of the code was taken directly from the CGIHTTPServer file,
nothing changed


I did the same, it is working for me. About Mac problems, the only thing I
know is that in the code of CGIHTTPServer itself a comment says that
execfile() is the only way to run a CGI on Mac. But it should work.

Maybe it's just your browser, which doesn't show you the output of the
CGI. Did you try connecting to the webserver by telnet?
--
Saluti,
Mardy
http://interlingua.altervista.org

Dec 2 '05 #6
I am using execfile, setting stdin and stdout like this:
sys.stdin = self.wfile
sys.stdout = self.rfile
execfile(filename)

Its the same code used in the CGIHTTPServer module. I know that the
python is executing corretly, a script with this content would work:

print "<html>"
print "<head>"
print "<title>"
print "blah"
print "</title>"
print "</head>
print "<body>"
print "test"
print "</body>"
print "</html>"

but this would not (lets say i submitted a form with the value of test
being "hello world"):

import cgi
form = cgi.FieldStorage()
print form["test"]
print "test"

I would only be able to see "test", not "hello world"
I am sure its not my browser

Dec 2 '05 #7
"amfr" <am******@gmail.com> wrote:

import cgi
form = cgi.FieldStorage()
print form["test"]
print "test"

I would only be able to see "test", not "hello world"
I am sure its not my browser


Did you mean:

print form["test"].value
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 4 '05 #8
Le die Fri, 02 Dec 2005 12:18:28 -0800, amfr ha scribite:
import cgi
form = cgi.FieldStorage()
print form["test"]
print "test"

I would only be able to see "test", not "hello world"
I am sure its not my browser


As Tim said, you have tu use "form['test'].value", because "print
form['test']" will make pythoun output something like this:
<form object at ....>
and your browser won't show it, mistaking it as an HTML tag.
--
Saluti,
Mardy
http://interlingua.altervista.org

Dec 4 '05 #9
Neither work

Dec 16 '05 #10
amfr wrote:
Neither work

But you don't give us any further information to go on.

Are you importing cgitb and calling cgitb.enable() to trap and print any
errors that might occur?

Are you looking in the browser at the HTM source (view source) of the
page your server is returning to see more closely waht your browser is
receiving, rather than what it's displaying?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 17 '05 #11

amfr wrote:
I am writing a webserver, and I want it to be able to run python
scripts. But when I set sys.stdin to self.rfile (using the
BaseHTTPServer class, self.rfile is a input stream containing the
request), the cgi module does not parse the data.
Example script:
import cgi
form = cgi.FieldStorage()
print form["testfield"]

The above script would print nothing. Should i be setting sys.stdin to
something else, or is there anthoer solution entirely?

try this before trying to print:
print "Content-Type: text/html\n"

Dec 17 '05 #12
"amfr" <am******@gmail.com> wrote:

Neither work


Yes, they do.

Post your form HTML and the Python code you're using, and we'll show you
what you're doing wrong.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago...
15
by: Florian Lindner | last post by:
Hello, I've one problem using the csv module. The code: self.reader = csv.reader(f, delimiter = ",") works perfectly. But when I use a variable for delimiter: self.reader = csv.reader(f,...
5
by: qqcq6s59 | last post by:
Hi all I am a newbie and I just saw a ongoing thread on Fileprocessing which talks abt config parser. I have writen many pyhton program to parse many kind of text files by using string module and...
6
by: Bob Alston | last post by:
I am looking for Access reporting add-in that would be easy to use by end users. My key focus is on selection criteria. I am very happy with the Access report writer capabilities. As far as...
4
by: Paul McGuire | last post by:
I have a new enhancement to pyparsing that doubles the parse speed (using a technique called "packrat parsing"), but which is not suitable for all parsers, specifically those that have complex...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
9
by: Ramashish Baranwal | last post by:
Hi, I want to get a module's contents (classes, functions and variables) in the order in which they are declared. Using dir(module) therefore doesn't work for me as it returns a list in...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
0
by: norseman | last post by:
mercado mercado wrote: ========================================================== Yes, but you may not like it. I do what you do. Prod and devel subdirs. I store the paths in a var at the top...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.