Build a simple local HTTP server with CGI in Python.
I tried to write a CGI script in Python instead of Perl if it was simple.
- CGI is an abbreviation for Common Gateway Interface, which is a function to display the result of processing programmatically on a Web server. *
(Quoted from What is CGI)
In short, I think that it is a function that can create a dynamic web page, fetch a value from a browser, process it on the server side, and return it again.
First of all, you have to set up a server, so set up an HTTP server locally and display a web page.
Write Index.html in the directory you want to work with to create the page you want to display. This time, the data to be sent will be food and season.
index.html
<html>
<head>
<title>Server test</title>
<meta http-equiv="content-type" charset="utf-8">
</head>
<body>
<form action="/cgi-bin/cgi_test.py" method="POST">
<div>
<label for="name">Favorite food</label>
<input type="text" name="food" value="Apple">
<label for="season">Favorite season</label>
<input type="text" name="season" value="winter">
<button>Send</button>
</div>
</form>
</body>
</html>
What you have to pay attention to here is the path of the action
"/cgi-bin/CGI script file name"
Is to be. This is the relative path to the CGI script file, which will be used later to create the script file using the script file name specified here.
Try starting the server once. Start it in the terminal.
$ python3 -m http.server 8080
When executed, it will look like the one below. I used 8080 for the time being, but any other number is fine as long as it can be used.
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
Now, check if it works. http://localhost:8080/ If all goes well, it will look like this: By the way, even if you press the submit button, you should still get an error.
Now, let's write the CGI script in Python. Create a directory named cgi-bin in the working directory (the directory where index.html is located), and write the CGI script in it. The file structure looks like this.
.
├── cgi-bin
│ └── cgi_test.py
└── index.html
Also, for the file name here, use the name specified earlier in index.html.
cgi_test.py
#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()
print("Content-Type: text/html; charset=utf-8\n\n")
print("<html><body>")
form = cgi.FieldStorage()
for key in form:
value = form[key].value
print('<p>%s: %s</p>' % (key, value))
print("</body></html>")
Be sure to write #! / Usr / bin / env python
on the first line here.
If you do not write this, you will get the error `ʻOS Error: [Errno 8] Exec format error: ``.
This specifies an interpreter, but please refer to the following articles for details.
#! / Bin / sh is not just a comment! Shebang! (Qiita)
Let's do it.
Start the server in the terminal as before.
Now add the --cgi
option to run CGI.
python3 -m http.server 8080 --cgi
After confirming the startup at http: // localhost: 8080 /, press Send this time to confirm. If it can be displayed, it is successful.
If you do not see it when you press the submit button, first check the terminal to see what kind of error you are getting.
code 403, message CGI script is not executable
→ Enter chmod 755 CGI script name.py
in the terminal on the cgi-bin directory.
SyntaxError: Non-ASCII character '\xe5'
→ Add # coding: utf-8
to the top of the file.
Build a comfortable web server with python and test CGI. (Qiita)
Recommended Posts