Python | Simple HTTP Server Construction

Simple HTTP server setup


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

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

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

Elements show the structure of a web page
Network shows the communication between the browser and the server
Click Network, make sure the first little red light is on, and Chrome will log all communications between the browser and the server:

image.png

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

image.png

import socket

# HTTP servers are all TCP-based socket connections
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 with two elements in the tuple
# The 0th element is the client's socket link
# The first element is the ip address and port number of the client
client_socket, client_addr = server_socket.accept()

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

# return message to client
client_socket.send('hello world'.encode('utf8'))
print(data)
The result of accessing in the browser is as follows:

image.png

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

import socket

# HTTP servers are all TCP-based socket connections
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 with two elements in the tuple
# The 0th element is the client's socket link
# The first element is the ip address and port number of the client
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

# 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, wrap the line
client_socket.send(' '.encode('utf8'))

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

image.png

Change the address in the code to 0.0.0.0:

import socket

# HTTP servers are all TCP-based socket connections
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 with two elements in the tuple
# The 0th element is the client's socket link
# The first element is the ip address and port number of the client
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

# 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, wrap 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's address in the browser. The access effect is as follows:

image.png

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

image.png

The code at this time can only receive a request once, you can add an infinite loop in 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 TCP-based socket connections
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 with two elements in the tuple
# The 0th element is the client's socket link
# The first element is the ip address and port number of the client
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

# 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, wrap the line
client_socket.send(' '.encode('utf8'))

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

image.png

Visit the result in the browser:

image.png

Adjust the returned content to the visitor's address:

import socket

port = int(input('Please enter the port number:'))
# HTTP servers are all TCP-based socket connections
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 with two elements in the tuple
# The 0th element is the client's socket link
# The first element is the ip address and port number of the client
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

# 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, wrap 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