Simple Http Server Construction | Teach You to Get Started With Python One -hundred and Ten

HTTP protocol: HyperText Transfer Protocol Hypertext transfer protocol, the function of the protocol is to transmit hypertext HTML ( HyperTextMarkupLanguage ).

Simple HTTP server setup

HTTP protocol: HyperText Transfer Protocol
The function of the HyperText Transfer Protocol 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 webpage and select Inspect to display the developer tools, among which:
•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:

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

import socket

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

server_ socket.bind (( '' , 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 ' ))
The result of accessing in the browser is as follows:
The browser returns a bad response, indicating that the response header needs to be set first

The access header and visitor address information will be printed out in the cloud server :
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:

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