I tried studying on the WEB server side at an in-house Python study session

I introduced the program on the WEB server side at the in-house study session for beginners (3rd time), so I wrote it here as well.

Environment: windows10, python3.7

Python provides a simple web server library. You can start the WEB server with just a command.

This time, we will implement it using CGI, which is easy to develop. CGI is a mechanism that processes requests from the browser.

Since the following path where the command is issued will be published, at the place you want to publish Execute the following command.

>python -m http.server 8888 --cgi

For example d:\work\py_test
When executed in the above path Put html in d: \ work \ py_test
Place the python file to be called in d: \ work \ py_test \ cgi-bin .

In the case of a business system, a database (hereinafter referred to as DB) is used to store data on the server side. This time, it takes time to prepare the DB, so I implemented it by storing the data in a file.

In Python, both file operations and DB operations can be easily implemented. The operation is similar Open → use → close It is a procedure of.

Create the following source file.

↓ Place in d: \ work \ py_test \

file_tesl.html


<html>
	<head><meta http-equiv="content-type" charset="utf-8"></head>
	<body>
File operation test
	<br>
	<form action="http://localhost:8888/cgi-bin/cgi_file_test.py" method="get">
	    <div>Enter name<input name="name" id="name" value=""></div>
	    <button>Run</button>
	</form>
	</body>
</html>

↓ Place in d: \ work \ py_test \ cgi-bin \

cgi_file_test.py


import cgi
import os

#Receiving parameters
form = cgi.FieldStorage()
str_name = form["name"].value

#Export to file (addition mode)
f = open('./data/test.txt','a')
f.write(str_name + "\n")
f.close()

#Read from file
read_str = ""
with open('./data/test.txt','r') as f:
    for row in f:
       read_str = read_str +"<br>"+ row.strip()

#Output in html
print ("Content-Type: text/html")
print ()
print ("<html><body>")
print ("The names you have entered so far are",read_str,"<br>")
print ("<a href=\"../file_test.html\">Return</a>")
print ("</body></html>")

Enter the following URL in your web browser and display it to check the operation.

http://locahost:8888/file_tesl.html

After displaying it on your web browser, enter your name and press "Run". After the screen transition, press "Back" and Let's enter the name.

You can see that it will be added.

In this way, Python not only makes cgi easy, but also makes file operations super easy.

Since the framework is used in the actual business WEB system, it is not hobo to write such a cgi, but since you can easily realize the WEB system, why not try various things?

Recommended Posts

I tried studying on the WEB server side at an in-house Python study session
Read "Ordinary Linux Programming" at an in-house study session
I tried Python on Mac for the first time.
I tried python on heroku for the first time
We held an in-house study session on mob programming with the theme of FizzBuzz.
Read "Ordinary Linux Programming" at an in-house study session ~ 10.11 Exercises ~
Test.py is not reflected on the web server in Python3.
Image Optimize on the server side using TinyPNG's Web API
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried web scraping with python.
I tried sending an email from the Sakura server with flask-mail
I tried to push the Sphinx document to BitBucket and it will be automatically reflected on the web server
I want to pass an argument to a python function and execute it from PHP on a web server
Get the width of the div on the server side with Selenium + PhantomJS + Python
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried using "Streamlit" which can do the Web only with Python
During the New Year holidays, I will study Python at Soshimu's Anaconda.
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
An example of the answer to the reference question of the study session. In python.
I tried the asynchronous server of Django 3.0
Python: I tried the traveling salesman problem
Matters examined at the August 2017 study session
Publish the current directory on the web server
I tried the Python Tornado Testing Framework
[Python] How to save images on the Web at once with Beautiful Soup
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)
I tried "smoothing" the image with Python + OpenCV
I tried web scraping to analyze the lyrics.
[Python] I tried substituting the function name for the function name
I tried web scraping using python and selenium
Drawing tips with matplotlib on the server side
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
Matters examined at the study session in February
I tried using PyCaret at the fastest speed
I tried python programming for the first time.
I tried launching jupyter nteract on heroku server
I tried "binarizing" the image with Python + OpenCV
I tried using the Datetime module by Python
Matters examined at the study session in May
At the time of python update on ubuntu
I tried sending an email with SendGrid + Python
[Python] I will upload the FTP to the FTP server.
I tried playing with the calculator on tkinter
A story about an engineer who came only on the server side created a portfolio
[Python3] Take a screenshot of a web page on the server and crop it further
A python beginner tried to intern at an IT company [Day 3: Going to the clouds ...]
[CentOS 7.3] Build an FTP server on the ESXi host
I tried to graph the packages installed in Python
I tried running the app on the IoT platform "Rimotte"
I tried to implement Minesweeper on terminal with python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
Continuation ・ I tried to make Slackbot after studying Python3
I tried to implement an artificial perceptron with python
[Python] I tried to graph the top 10 eyeshadow rankings
Execute the command on the web server and display the result
[Python] I tried running a local server using flask
I tried to solve the problem with Python Vol.1
I tried input interpolation on UE4 Python VS Code