开发者

How do I raise an http error/exception from a python CGI script?

How do I raise an http error/exception from a python CGI script?

Is all that is necessary is to print the appropriate header:

print '''Status: 501 Not Implemented
Content-type: text/html

'''

That doesn't seem to work right.

I have a very basic setup, namely IIS7 routing *.py C开发者_如何学JAVAGI scripts to python25.exe for execution. I am not using any WSGI or FastCGI. Using the "normal" CGI modules: cgitb and cgi


It seems like that's the way to do it. As long as you're correctly following the header format.

Here's someone else who asked the same question, more or less.

Returning http status codes in Python CGI

Are you following HTTP specs when you print the status code? Can you try printing just the status code and not its description?

Maybe like...

print '''Status:501
Content-type: text/html

'''

Or it should be like...

print '''HTTP/1.1 501 Not Implemented
Content-type: text/html

'''

Could you test with your setup to verify?

Returning status from CGI:

http://oreilly.com/openbook/cgi/ch03_07.html

I guess "Status: 501 Not Implemented" like you had it originally is the way to go. I don't know why it's not working. Are you printing any non-header content before printing the status code?

Yet another source that confirms you're doing it right (look in section 6.3.3.):

http://www.ietf.org/rfc/rfc3875

EDIT 1,2,3: extended answer


Start your script with: cgitb.enable() Then to output a correct error code during an exception use:

try:
  blah blah failed code
except:
  print "Status: 500"
  cgitb.handler()


Python CGI returning an http status code, such as 403?

One answer suggested there:

sys.stdout('Status: 403 Forbidden\r\n\r\n')

may be technically more correct, according to RFC (assuming that your CGI script isn't running in text mode on Windows). However both line endings seem to work everywhere.

But since you're doing this on windows and you're not ending the header with the status line...

sys.stdout('Status: 501 Not Implemented\n')

I don't see why using sys.stdout would matter though since print should be using stdout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜