问题描述
我的python cgi如何返回特定的http状态代码,例如403或418?
How can my python cgi return a specific http status code, such as 403 or 418?
我尝试了显而易见的(打印状态:403 Forbidden),但它不起作用。
I tried the obvious (print "Status:403 Forbidden") but it doesn't work.
推荐答案
print 'Status: 403 Forbidden'
print
适合我。您确实需要第二次打印,因为您需要一个双换行来结束HTTP响应标头。否则,您的网络服务器可能会抱怨您没有向它发送一套完整的标题。
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')
在技术上可能更正确,根据RFC(假设您的CGI脚本未在Windows上以文本模式运行)。然而,两个行结尾似乎无处不在。
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.
这篇关于Python CGI返回一个http状态代码,如403?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!