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

本秂侑毒 提交于 2019-12-17 20:35:00

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1411867/python-cgi-returning-an-http-status-code-such-as-403

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!