Create a cgi-bin
directory directly under your home directory.
user@hostname:~$ mkdir cgi-bin
user@hostname:~$ cd cgi-bin
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>')
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. charset
is an explicit declaration of the character code of the file.print ('Content-Type: text / html; charset = utf-8 \ n \ n')
. ..The procedure for executing CGI is explained.
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)
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.
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.
--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