This article is Zenn's online book
"Introduction to self-made Python web applications for third-year web engineers who are sluggish"
This is a partial excerpt from.
Here https://qiita.com/bigen1925/items/dde5575da6c45a91808d Please also read.
Suddenly, let's create a server that receives a request from a browser in Python and returns a response to the browser.
The following program starts as a server on port 8080 (localhost: 8080
) of your PC, writes the contents to a file called server_recv.txt
when receiving a request from the browser, and then server_send.txt
Reads the contents of the file and returns it to the browser as a response.
TCPServer.py
import socket
class TCPServer:
"""
A class that represents a server that performs TCP communication
"""
def serve(self):
"""
Start the server
"""
print("===Start the server===")
try:
#Generate socket
server_socket = socket.socket()
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#Assign socket to localhost port 8080
server_socket.bind(("localhost", 8080))
server_socket.listen(10)
#Wait for a connection from the outside and establish a connection if there is a connection
print("===Wait for a connection from the client===")
(client_socket, address) = server_socket.accept()
print(f"===The connection with the client is complete remote_address: {address} ===")
#Get the data sent from the client
request = client_socket.recv(4096)
#Write the data sent from the client to a file
with open("server_recv.txt", "wb") as f:
f.write(request)
#Get the response data to be sent to the client from the file
with open("server_send.txt", "rb") as f:
response = f.read()
#Send a response to the client
client_socket.send(response)
#End communication
client_socket.close()
finally:
print("===Stop the server.===")
if __name__ == '__main__':
server = TCPServer()
server.serve()
Let's also create a server_send.txt
for return.
server_send.txt
HTTP/1.1 200 OK
Date: Wed, 28 Oct 2020 07:57:45 GMT
Server: Apache/2.4.41 (Unix)
Content-Location: index.html.en
Vary: negotiate
TCN: choice
Last-Modified: Thu, 29 Aug 2019 05:05:59 GMT
ETag: "2d-5913a76187bc0"
Accept-Ranges: bytes
Content-Length: 45
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
<html><body><h1>It works!</h1></body></html>
Move to the directory where the source code is placed on the console, and execute the program with the following command.
$ python TCPServer.py
===Start the server===
===Wait for a connection from the client===
Keep the console tab open and try accessing http: // localhost: 8080
in your browser.
It was displayed as a web page safely!
Except for comments, I was able to run a fairly primitive web server in just over a dozen lines. (At the moment, it is more accurate to call it a TCP server, not a white server that can be called a Web server ...)
The best way to understand how everything works is to make it yourself.
If you want to understand how a web server or web framework works, please come to our online book. https://zenn.dev/bigen1925/books/e6c9492a82f5e2e10fca/viewer/504d96 Please read!
There is also a description of the source code for this article.
Recommended Posts