cgi problem

P

Paul Rubin

I'm trying to write a simple cgi that reads a post from the user's
browser, does some stuff with the form data, and redirects the browser
back to the originating url, i.e. I want the cgi to send a 302
redirect.

There's no obvious way in the cgi module to set the response code to
anything but 200. I.e. the run_cgi method automatically sends the 200
response without giving any way to suppress it (like nph-whatever in
Apache). Is that a missing feature that I should add, or am I trying
to do something dumb?

Thanks.
 
K

Kent Johnson

Paul said:
I'm trying to write a simple cgi that reads a post from the user's
browser, does some stuff with the form data, and redirects the browser
back to the originating url, i.e. I want the cgi to send a 302
redirect.

There's no obvious way in the cgi module to set the response code to
anything but 200. I.e. the run_cgi method automatically sends the 200
response without giving any way to suppress it (like nph-whatever in
Apache). Is that a missing feature that I should add, or am I trying
to do something dumb?

Set the Location: header to the new URL.
http://hoohoo.ncsa.uiuc.edu/cgi/out.html
http://www.algonet.se/~ug/html+pycgi/redirect.html

Kent
 
T

Thomas Guettler

Am Wed, 08 Mar 2006 00:19:55 -0800 schrieb Paul Rubin:
I'm trying to write a simple cgi that reads a post from the user's
browser, does some stuff with the form data, and redirects the browser
back to the originating url, i.e. I want the cgi to send a 302
redirect.

Hi,

I have this setup for a small script (for bigger things I use quixote)



def cgi_main():
stdout=sys.stdout
sys.stdout=sys.stderr # print soll zu Apache-Log
try:
html=cgi_html()
except ReturnThis, r:
stdout.write(str(r))
return
stdout.write("Content-Type: text/html\n\n%s" % html)

CGI-Script *very* small
............
# Python Imports
import os
import sys

import cgitb
cgitb.enable()
import foo

if __name__=="__main__":
foo.cgi_main()

.............

file foo:

def cgi_main():
stdout=sys.stdout
sys.stdout=sys.stderr # print should go to Apache-Log
try:
html=cgi_html()
except ReturnThis, r:
stdout.write(str(r))
return
stdout.write("Content-Type: text/html\n\n%s" % html)


class ReturnThis(Exception):
pass

class Redirect(ReturnThis):
def __init__(self, destination):
if os.environ.get("HTTPS")=="on":
http="https://"
else:
http="http://"
url="%s%s%s%s" % (http, os.environ["SERVER_NAME"], os.environ["SCRIPT_NAME"],
destination)
header="Status: 302 Moved Temporarily\nLocation: %s\n\n" % (
url)
ReturnThis.__init__(self, header)


Now you can 'raise Redirect("mylocation")' anywhere in your code.

HTH,
Thomas
 
P

Paul Rubin

Thomas Guettler said:
I have this setup for a small script (for bigger things I use quixote)...

Thanks. It looks like you've written your cgi completely from
scratch. I was hoping to use the cgi module, which has some
convenient features for reading the query parameters and POST content.
I should look into Quixote and some of the other Python web frameworks
but this particular task is pretty simple so I thought I'd just use a cgi.
 
T

Tim Roberts

Paul Rubin said:
Thanks. It looks like you've written your cgi completely from
scratch. I was hoping to use the cgi module, which has some
convenient features for reading the query parameters and POST content.

Yes, but the CGI module doesn't write anything, so the advice of writing a
"Location:" header still applies.
 
P

Paul Rubin

Tim Roberts said:
Yes, but the CGI module doesn't write anything, so the advice of writing a
"Location:" header still applies.

Aha, it's coming from CGIHTTPServer.py:CGIHTTPRequestHandler.run_cgi()
where it says

self.send_response(200, "Script output follows")

I got the two modules confused. This still leaves me with the same
basic problem, how to suppress the sending of that header.

Thanks.
 
K

Kent Johnson

Paul said:
Aha, it's coming from CGIHTTPServer.py:CGIHTTPRequestHandler.run_cgi()
where it says

self.send_response(200, "Script output follows")

I got the two modules confused. This still leaves me with the same
basic problem, how to suppress the sending of that header.

Ah, now I get it. This does look like a bug in CGIHTTPServer to me. As
you note, it hardcodes the 200 status response. The CGI spec says that
the CGI program can use the Status: header to tell the server what
status code to send but CGIHTTPServer doesn't do that.

ISTM the spec requires the server to buffer and interpret the HTTP
headers from the CGI so the Status header can be set based on the CGI
response.
 
T

Thomas Guettler

Am Thu, 09 Mar 2006 00:35:25 -0800 schrieb Paul Rubin:
Aha, it's coming from CGIHTTPServer.py:CGIHTTPRequestHandler.run_cgi()
where it says

self.send_response(200, "Script output follows")

I got the two modules confused. This still leaves me with the same
basic problem, how to suppress the sending of that header.

I had this problem, too:

https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1055159&group_id=5470

CGIHTTPServer writes "200" before the script gets executed!

You can return this:
"""<html>
<head>
<meta http-equiv="refresh"
content="1; url=...">
</head>
</html>"""


Thomas
 

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,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top