17

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

2 Answers2

24

via cgi script?

print "Status:301\nLocation: http://www.google.com"
Chris Huang-Leaver
  • 6,059
  • 6
  • 41
  • 67
Jiri
  • 16,425
  • 6
  • 52
  • 68
  • can you also post http request handler in this case/ – Deepak Ingole Aug 18 '14 at 12:31
  • 2
    Note also that this won't work if you're running your script via Python's built-in CGIHTTPServer http://stackoverflow.com/a/32079589/227651 – Mike Howsden Aug 18 '15 at 18:13
  • @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. – Jiri Aug 21 '15 at 09:27
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 []
S.Lott
  • 384,516
  • 81
  • 508
  • 779