Operation check
Python 2.7
Mac OS
$ mkdir cgi-bin $ vim cgi-bin/redirect.py
redirect.py
#! /usr/bin/python
print "Content-type: text/html;\n\n"
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://qiita.com/\">"
$ chmod +x cgi-bin/redirect.py $ python -m CGIHTTPServer 8000
Access localhost: 8000 / cgi-bin / redirect.py in your browser
Normally you can redirect using Location, According to Python 2.7.12 documentation, it is not possible to redirect by writing in HTTP response, so give up and another WEB server I tend to use.
Note CGI scripts executed in the CGIHTTPRequestHandler class cannot be redirected (code 302) because they output HTTP code 200 (script output follows) prior to execution (this is the status code). ..
However, it can also be achieved in a way like this one. Whether it can be done and whether it is used is a different story.
Confirmed in Python 2.7 environment. However, it is not a technology that makes a difference in small versions. In the case of 3 series, it is this, but the idea of redirect is the same.
Also, I've cut various things to show the minimum procedure. Please write properly in each.
Recommended Posts