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.
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
> 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
--
Thomas Güttler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: niemand....@thomas-guettler.de
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.
Yes, but the CGI module doesn't write anything, so the advice of writing a
"Location:" header still applies.
--
- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
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.
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.
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>"""