Receive and display HTML form data in Python

I received the HTML form data in Python and tried to display it.

HTML file description

First, write the code to submit the form in HTML.

index.html


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <form method="POST" action="cgi-bin/index.py">
      <label>text:</label>
      <textarea name="text"></textarea>
      <button type="submit">Send</button>
    </form>
  </body>
</html>

--About method

The method has a GET method to get the data and a POST method to send the data. This time, we will send the data, so we will use the POST method.

--About ʻaction`

You can specify the URL to send the data with ʻaction. This time, send it to cgi-bin / index.py` that will appear later.

For more information on submitting form data, see the following articles. https://developer.mozilla.org/ja/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data

Python file description

Next, write a CGI (Gateway Interface Standard) script that receives and displays data in Python. A CGI script is a script that is started by an HTTP server and processes data entered by the user in HTML or the like. For details, refer to the following article. https://docs.python.org/ja/3/library/cgi.html#module-cgi

cgi-bin/index.py


#!usr/bin/python
# -*- coding: utf-8 -*-

import cgi #Import CGI module
import cgitb
import sys

cgitb.enable() #Since it is used for debugging, it is not described in the production environment.

form = cgi.FieldStorage() #Get form data

print("Content-Type: text/html; charset=UTF-8") #Header for writing HTML
print("")

#If no form data has been entered
if "text" not in form:
    print("<h1>Error!</h1>")
    print("<br>")
    print("Enter the text!")
    print("<a href='/'><button type='submit'>Return</button></a>")
    sys.exit()

text = form.getvalue("text") #Get the value of the data

print(text)

Change the execution authority with the following command for later execution.

Command


$ chmod 755 cgi-bin/index.py

Run CGI server

(I referred to the following article. https://qiita.com/shuichi0712/items/5ddc5b4e30c2373b17fb )

Next, run the CGI server. Write the script for that in cgiserver.py.

cgiserver.py


import http.server
http.server.test(HandlerClass=http.server.CGIHTTPRequestHandler)

After that, execute the following command to run the CGI server.

Command


$ python -m http.server --cgi

After that, if you search for http://0.0.0.0:8000 in the browser, the form will be displayed, and if you fill out and submit the form, the input contents will be displayed.

Recommended Posts

Receive and display HTML form data in Python
Display UTM-30LX data in Python
View photos in Python and html
Hashing data in R and Python
Check and receive Serial port in Python (Port check)
Easily graph data in shell and Python
Python variables and data types learned in chemoinformatics
[Python] Swapping rows and columns in Numpy data
Display candlesticks for FX (forex) data in Python
Submit in [Python] form
POST JSON in Python and receive it in PHP
Let's play with Python Receive and save / display the text of the input form
Full-width and half-width processing of CSV data in Python
Receive dictionary data from a Python program in AppleScript
Display HTML in Jupyter notebook
Receive runtime arguments in Python 2.7
Stack and Queue in Python
Unittest and CI in Python
Application to display and search local memos (diary) in Python
Display numbers and letters assigned to variables in python print
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Graph time series data in Python using pandas and matplotlib
Just try to receive a webhook in ngrok and python
Get Leap Motion data in Python.
Python: Exclude tags from html data
MIDI packages in Python midi and pretty_midi
Difference between == and is in python
Read Protocol Buffers data in Python3
Sorting algorithm and implementation in Python
Get data from Quandl in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Handle NetCDF format data in Python
Assignments and changes in Python objects
Waveform display of audio in Python
Display characters like AA in python
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
processing to use notMNIST data in Python (and tried to classify it)
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
How to display bytes in the same way in Java and Python
Overlapping regular expressions in Python and Java
Display LaTeX notation formulas in Python, matplotlib
Get additional data in LDAP with python
Data pipeline construction with Python and Luigi
[Python, Julia] 3D display in Jupyter-Mayavi library
Calculate and display standard weight with python
Differences in authenticity between Python and JavaScript
Receive textual data from mysql with python
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Data input / output in Python (CSV, JSON)
AM modulation and demodulation in Python Part 2