Let's play with Python Receive and save / display the text of the input form

Whether it's Python or whatever, it's not interesting and won't continue unless you make something that works for the time being. Let's create a simple submission form on a rental server such as Sakura Internet where Python scripts run on CGI.

sendtext.cgi



#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
from datetime import datetime
import cgi
import csv

print "Content-Type: text/html\n"


print """

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
"""

print """
</head>
<body>
"""

try:
    form = cgi.FieldStorage()
    words = form["word"].value
    words = unicode(words,"utf-8")
    today = datetime.now().strftime("%Y/%m/%d %H:%M:%S")

    f = open("date.txt", "ab")
    writer = csv.writer(f, quoting=csv.QUOTE_ALL)
    writer.writerow([words,today])
    f.close()

    print "<p>"+ words + ": " + today + "</p>"

except (TypeError, KeyError):
    print "<p>"+u"There is an error in the input item"+"</p>"

print """
</body>
</html>
"""

Write a Python script with this image. The extension is .cgi because it is supposed to work with CGI. It is a simple script that receives the form sent from index.html, attaches the date and time it was sent to it, and saves it in CSV format in .txt.

HTML file

<form method="POST" action="cgi-bin/index.cgi">
  <textarea name="word"></textarea>
  <p><input type="submit" value="Send"></p>
</form>

Such an image would be OK.

Recommended Posts

Let's play with Python Receive and save / display the text of the input form
Play with the password mechanism of GitHub Webhook and Python
Receive and display HTML form data in Python
Receive the form in Python and do various things
Visualize the range of interpolation and extrapolation with python
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
[Let's play with Python] Image processing to monochrome and dots
The story of Python and the story of NaN
Let's play with Excel with Python [Beginner]
Coexistence of Python2 and 3 with CircleCI (1.0)
I compared the speed of Hash with Topaz, Ruby and Python
Speed comparison of Wiktionary full text processing with F # and Python
[Python3] Save the mean and covariance matrix in json with pandas
[Let's play with Python] Aiming for automatic sentence generation ~ Completion of automatic sentence generation ~
Let's summarize the degree of coupling between modules with Python code
Operate Firefox with Selenium from python and save the screen capture
Calculate and display standard weight with python
I replaced the numerical calculation of Python with Rust and compared the speed
[Python] How to specify the window display position and size of matplotlib
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Check the existence of the file with python
POST variously with Python and receive with Flask
Display Python 3 in the browser with MAMP
Fractal to make and play with Python
Let's read the RINEX file with Python ①
Send and receive image data as JSON over the network with Python
Find the general terms of the Tribonacci sequence with linear algebra and Python
Receive a list of the results of parallel processing in Python with starmap
Get the number of articles accessed and likes with Qiita API + Python
Get and estimate the shape of the head using Dlib and OpenCV with python
I measured the speed of list comprehension, for and while with python2.7.
Execute raw SQL using python data source with redash and display the result
Let's simulate the transition of infection rate with respect to population density with python
Learn the trends of feature words in texts with Jubatus and categorize input texts
Competitive Pro with Python and VSCode-Simplification of standard input and automation of sample case judgment-
Let's automatically display the lyrics of the song being played on iTunes in Python
Let's print PDF with python using foxit reader and specify the printer silently!
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Try out the touch of data-driven testing with Selenium Python Bindings and py.test
Get the stock price of a Japanese company with Python and make a graph
Python hand play (let's get started with AtCoder?)
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
The answer of "1/2" is different between python2 and 3
Calculate the total number of combinations with python
Specifying the range of ruby and python arrays
Interactively display algebraic curves with Python and Jupyter
Check the date of the flag duty with Python
Compare the speed of Python append and map
Implementation of TRIE tree with Python and LOUDS
Let's control EV3 motors and sensors with Python
Solving the Lorenz 96 model with Julia and Python
Output in the form of a python array
Archive and compress the entire directory with python
Convert the character code of the file with Python3
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
Continuation of multi-platform development with Electron and Python
[Python] Determine the type of iris with SVM
Example of reading and writing CSV with Python