CGI module does not parse data

A

amfr

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?
 
F

Fredrik Lundh

"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>
 
A

amfr

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
 
A

amfr

I just read somewhere that the CGIHTTPServer module does not work on
mac (which I am currently using), is this true?
 
P

Peter Hansen

amfr said:
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
 
M

Mardy

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?
 
A

amfr

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
 
T

Tim Roberts

amfr said:
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
 
M

Mardy

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.
 
S

Steve Holden

amfr said:
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
 
D

Devan L

amfr said:
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top