17

Is it possible to send a status code other than 200 via a python cgi script (such as 301 redirect)

2 Answers 2

24

via cgi script?

print "Status:301\nLocation: http://www.google.com"
Sign up to request clarification or add additional context in comments.

3 Comments

can you also post http request handler in this case/
Note also that this won't work if you're running your script via Python's built-in CGIHTTPServer stackoverflow.com/a/32079589/227651
@MikeHowsden good point! It seems it is not possible to return status code with python built-in CGIHTTPServer. IMO it is major drawback because returning different status is very common even in quite simple webapps. Fortunately, other web server's behaviour is not the same.
0

Via wsgi application?

def simple_app(environ, start_response):
    status = '301 Moved Permanently' # HTTP Status
    headers = [('Location','http://example.com')] # HTTP Headers
    start_response(status, headers)

    return []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.