Python CGI returning an http status code, such as

2019-01-19 10:50发布

How can my python cgi return a specific http status code, such as 403 or 418?

I tried the obvious (print "Status:403 Forbidden") but it doesn't work.

2条回答
仙女界的扛把子
2楼-- · 2019-01-19 11:22

I guess, you're looking for send_error. It would be located in http.server in py3k.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-19 11:33
print 'Status: 403 Forbidden'
print

Works for me. You do need the second print though, as you need a double-newline to end the HTTP response headers. Otherwise your web server may complain you aren't sending it a complete set of headers.

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.

查看更多
登录 后发表回答