473,471 Members | 4,650 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cgi problem

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.
Mar 8 '06 #1
7 1534
Paul Rubin wrote:
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
Mar 8 '06 #2
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: ni**************@thomas-guettler.de

Mar 8 '06 #3
Thomas Guettler <ni**************@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.
Mar 8 '06 #4
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:

Thomas Guettler <ni**************@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.
Mar 9 '06 #5
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.
Mar 9 '06 #6
Paul Rubin wrote:
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.


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.
Mar 9 '06 #7
Am Thu, 09 Mar 2006 00:35:25 -0800 schrieb Paul Rubin:
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.


I had this problem, too:

https://sourceforge.net/tracker/?fun...&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

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

Mar 9 '06 #8

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

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.