[Python] Redirect with CGIHTTPServer

Operation check


Python 2.7
Mac OS

How to do

  1. Write .py that spits html that redirects with meta tag
  2. Launch CGI HTTP Server
  3. Access the created .py

$ 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

Background

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.

Supplement

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

[Python] Redirect with CGIHTTPServer
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
Redirect with Pyramid
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Web development with Python] query is also redirect
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib