Pass values between pages using Python 3.5 cgi module

Understand web apps with cgi modules

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

Folder structure

├── 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 cgiValueTest01.png

cgiValueTestresult.png

It became a feeling. What kind of communication result is obtained with the local proxy ... request localpost.png

response res01.png

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.

Summary

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.

Reference site

https://yxshipg.appspot.com/python/python3_cgi/

Recommended Posts

Pass values between pages using Python 3.5 cgi module
Cooperation between python module and API
Try using the Python Cmd module
Python> dictionary> values ()> Get All Values by Using values ()
Pass the path of the imported python module
[Python] Let's execute the module regularly using schedule
Refined search for Pokemon race values using Python
I tried using the Datetime module by Python
Try using the collections module (ChainMap) of python3
Let's make a module for Python using SWIG
Python: Tips-Swap values
Python module import
Python CGI basics
Start using Python
Python collections module
Scraping using Python
Pass the authentication proxy through communication using python urllib3
Error due to conflict between python when using gurobi
Write data to KINTONE using the Python requests module