Python | Simple HTTP server construction

Simple HTTP server construction

HTTP protocol: HyperText Transfer Protocol hypertext transfer protocol
The role of the protocol is to transmit hypertext HTML (HyperTextMarkupLanguage)

HTML: Hypertext Markup Language
HTTP: A protocol for transmitting hypertext

Taking Chrome as an example, right-click on the blank space of the web page and select Inspect to display the developer tools, among which:

Elements display the structure of web pages

Network shows the communication between the browser and the server
Click Network, make sure the first little red light is on, and Chrome will record all communication between the browser and the server:

General contains some overview information. The detailed process of the request header and response header during the request sending process is as follows:

import socket

# HTTP servers are all socket connections based on TCP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_socket.bind(('192.168.31.199', 9090))
server_socket. listen(128)

# The obtained data is a tuple, and there are two elements in the tuple
# The 0th element is the socket link of the client
# The first element is the client's ip address and port number
client_socket, client_addr = server_socket. accept()

# Get data from the client's socket
data = client_socket.recv(1024).decode('utf8')

# Return a message to the client
client_socket.send('hello world'.encode('utf8'))
print(data)
The results of accessing in the browser are as follows:

The browser feedbacks bad response, indicating that the response header needs to be set first

import socket

# HTTP servers are all socket connections based on TCP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_socket.bind(('192.168.31.199', 9090))
server_socket. listen(128)

# The obtained data is a tuple, and there are two elements in the tuple
# The 0th element is the socket link of the client
# The first element is the client's ip address and port number
client_socket, client_addr = server_socket. accept()

# Get data from the client's socket
data = client_socket.recv(1024).decode('utf8')
print('Received data{}'.format(data))

# Before returning the content, you need to set the HTTP response header first

# Set a response header and change a line
client_socket.send('HTTP/1.1 200 OK '.encode('utf8'))
client_socket.send('content-type: text/html '.encode('utf8'))

# After all the response headers are set, change the line
client_socket.send(' '.encode('utf8'))

# Send Content
client_socket.send('hello world'.encode('utf8'))
The results of accessing in the browser are as follows:

Change the address in the code to 0.0.0.0:

import socket

# HTTP servers are all socket connections based on TCP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_socket.bind(('0.0.0.0', 9090))
server_socket. listen(128)

# The obtained data is a tuple, and there are two elements in the tuple
# The 0th element is the socket link of the client
# The first element is the client's ip address and port number
client_socket, client_addr = server_socket. accept()

# Get data from the client's socket
data = client_socket.recv(1024).decode('utf8')
print('Received {} data{}'.format(client_addr[0], data))

# Before returning the content, you need to set the HTTP response header first

# Set a response header and change a line
client_socket.send('HTTP/1.1 200 OK '.encode('utf8'))
client_socket.send('content-type: text/html '.encode('utf8'))

# After all the response headers are set, change the line
client_socket.send(' '.encode('utf8'))

# Send Content
client_socket.send('hello world'.encode('utf8'))
Run this py file in the cloud server, and then access the corresponding port of the server address in the browser, the access effect is as follows:

The access header and visitor address information will be printed out in the cloud server:

The code at this time can only receive one request, you can add an infinite loop to the code, and change the port setting method to dynamic setting:

import socket

port = int(input('Please enter the port number:'))
# HTTP servers are all socket connections based on TCP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_socket.bind(('0.0.0.0', port))
server_socket. listen(128)
print('server is running at 0.0.0.0:{}'. format(port))

# The obtained data is a tuple, and there are two elements in the tuple
# The 0th element is the socket link of the client
# The first element is the client's ip address and port number
while True:
client_socket, client_addr = server_socket. accept()

# Get data from the client's socket
data = client_socket.recv(1024).decode('utf8')
print('Received {} data{}'.format(client_addr[0], data))

# Before returning the content, you need to set the HTTP response header first

# Set a response header and change a line
client_socket.send('HTTP/1.1 200 OK '.encode('utf8'))
client_socket.send('content-type: text/html '.encode('utf8'))

# After all the response headers are set, change the line
client_socket.send(' '.encode('utf8'))

# Send Content
client_socket.send('hello world'.encode('utf8'))
Start it in the server and use port 8090:

Access results in the browser:

Adjust the returned content to be the visitor's address:

import socket

port = int(input('Please enter the port number:'))
# HTTP servers are all socket connections based on TCP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_socket.bind(('0.0.0.0', port))
server_socket. listen(128)
print('server is running at 0.0.0.0:{}'. format(port))

# The obtained data is a tuple, and there are two elements in the tuple
# The 0th element is the socket link of the client
# The first element is the client's ip address and port number
while True:
client_socket, client_addr = server_socket. accept()

# Get data from the client's socket
data = client_socket.recv(1024).decode('utf8')
print('Received {} data{}'.format(client_addr[0], data))

# Before returning the content, you need to set the HTTP response header first

# Set a response header and change a line
client_socket.send('HTTP/1.1 200 OK '.encode('utf8'))
client_socket.send('content-type: text/html '.encode('utf8'))

# After all the response headers are set, change the line
client_socket.send(' '.encode('utf8'))

# Send Content
client_socket.send(client_addr[0].encode('utf8'))

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us