Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cgi problem

37 views
Skip to first unread message

Paul Rubin

unread,
Mar 8, 2006, 3:19:55 AM3/8/06
to
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.

Kent Johnson

unread,
Mar 8, 2006, 6:15:00 AM3/8/06
to

Thomas Guettler

unread,
Mar 8, 2006, 10:43:37 AM3/8/06
to
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

--
Thomas Güttler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: niemand....@thomas-guettler.de

Paul Rubin

unread,
Mar 8, 2006, 4:55:25 PM3/8/06
to
Thomas Guettler <niemand....@thomas-guettler.de> writes:
> > back to the originating url, i.e. I want the cgi to send a 302 redirect.
>
> 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.

Tim Roberts

unread,
Mar 9, 2006, 3:14:07 AM3/9/06
to
Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>
>Thomas Guettler <niemand....@thomas-guettler.de> writes:
>> > back to the originating url, i.e. I want the cgi to send a 302 redirect.
>>
>> 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.

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.

Paul Rubin

unread,
Mar 9, 2006, 3:35:25 AM3/9/06
to
Tim Roberts <ti...@probo.com> writes:
> 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.

Kent Johnson

unread,
Mar 9, 2006, 7:42:41 AM3/9/06
to

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.

Thomas Guettler

unread,
Mar 9, 2006, 10:55:25 AM3/9/06
to

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

0 new messages