Python CGI basics

Assumed environment

Create cgi-bin directory

Create a cgi-bin directory directly under your home directory.

user@hostname:~$ mkdir cgi-bin
user@hostname:~$ cd cgi-bin

Creating a CGI script

user@hostname:~/cgi-bin$ which python3
/usr/bin/python3

user@hostname:~/cgi-bin$ vi hello.py

Check the Python3 path with the which command.

hello.py


#!/usr/bin/python3

print('Content-Type: text/html; charset=utf-8')
print()

print('<h1>Hello, World!</h1>')

Code description

  1. You need to write a comment called Shebang on the first line.
    Shebang indicates the location of the compiler, without which Python code cannot be recognized as Python, leading to unexpected behavior.
  2. The third line print ('Content-Type: text / html; charset = utf-8') functions as an HTTP header and embeds the string output in the code inside the HTML BODY tag. be able to.
    The specification of charset is an explicit declaration of the character code of the file.
  3. The blank text of print on the 4th line is used because two line breaks are required for the HTTP header on the 3rd line to work properly.
    If you don't want to use blank text, you can do the same by writing the HTTP header as print ('Content-Type: text / html; charset = utf-8 \ n \ n'). ..

Run CGI

The procedure for executing CGI is explained.

  1. Grant execute permission to the cgi-bin directory and executable file.
  2. Start the python test server
  3. Access with a browser to check

Local development is possible with the above three steps.
If you want to publish it, you can publish it using Apache2. (For details, go to this article)

command

user@hostname:~$ ls
cgi-bin

user@hostname:~$ sudo chmod -R +x cgi-bin

python3 -m http.server 8080 --cgi
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...

Note that you need to start the server directly above the *** cgi-bin directory. *** *** Now that it is published on localhost, access [http: // localhost: 8080 / cgi-bin / hello.py](http: // localhost: 8080 / cgi-bin / hello.py). If it is displayed, it is successful.

error

404 Not Found --The path may be wrong because it is said that the file cannot be found. --Check if you have forgotten to start the server. --Check that the URL and file name are correct.

403 Forbidden --It is played because you do not have access authority. --Go back to the terminal and check the permissions with the ls -l command to see if x is attached. --If you are accessing http: // localhost: 8080 / cgi-bin, access it with a direct link to the file.

500 Internal Server Error --It is difficult to identify the cause because it is displayed as a wide range of errors due to server errors. --When you display the terminal that started the server, the access log is displayed under Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ..., so check the error details. And deal with it. --When checking the error log, it is easier to understand and can be resolved efficiently by reading from bottom to top.

The screen goes blank ・ The source code is displayed as it is

--Common --There is a high possibility that the description of the HTTP header is incorrect. --Check that the number of line breaks is correct and that you have not forgotten or made a mistake in the semicolon. --Or make sure you haven't made a mistake in the Shebang path or grammar. --The source code is displayed as it is --Check if you have forgotten --cgi when you start the server.

Recommended Posts

Python CGI basics
Python basics ⑤
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
Python
python: Basics of using scikit-learn ①
Run Python CGI on CORESERVER
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
SNS Python basics made with Flask
Getting Started with Python Basics of Python
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
Python CGI file created on Windows
About the basics list of Python basics
(python) Deep Learning Library Chainer Basics Basics
Learn the basics of Python ① Beginners
kafka python
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
python + lottery 6
Python Summary
Built-in python
Python comprehension
CGI server (1) python edition in one line
Studying python
Python 2.7 Countdown