I wasn't sure how to write the process of passing values between pages on python, so when I was looking at the python reference of the cgi module, I found the following description.
Use the FieldStorage class to get the completed form data. http://docs.python.jp/3.5/library/cgi.html
I tried to use the sample code of the above URL, but I don't know how to use it. I think the reference is difficult for beginners to understand.
So, when I was investigating how to pass values, many people wrote a combination of python code and html. If you think about it, you can understand it lol
├── cgi-bin │ └── cgiValueTest.py └── cgiserver.py └── index.html
index.html
<!DOCTYPE html>
<html>
<head>
<title>cgiValueTest.py</title>
</head>
<body>
<form action="/cgi-bin/cgiValueTest.py" method="POST">
<input type="text" name="text" value="diag" />
<input type="submit" name="submit" />
</form>
</body>
</html>
cgiValueTest.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import cgi
html_body = """
<!DOCTYPE html>
<html>
<head>
<title>Display received data</title>
<style>
h1 {
font-size: 3em;
}
</style>
</head>
<body>
<h1>%s</h1>
</body>
</html>
"""
form = cgi.FieldStorage()
text = form.getvalue('text','')
print(html_body % (text))
Please see the following article for how to run cgi server and cgiserver.py.
Try running cgi on Python3 http://qiita.com/shuichi0712/items/5ddc5b4e30c2373b17fb
When I run it
It became a feeling. What kind of communication result is obtained with the local proxy ... request
response
That is, cgi.FieldStorage () is written on the receiving side of the value. By the way, the FieldStorage () function converts the contents of the request (text = diag and submit =% 91% 97OM here) into Python strings. The converted data is used for processing. As a result, diag is displayed in the response.
It is quicker to understand by actually moving it than by thinking only with your head. Especially for beginners, it is the royal road to try it first and then understand what it means.
https://yxshipg.appspot.com/python/python3_cgi/
Recommended Posts