Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize static web Server with python

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to realize static web server with python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement a static web server with python".

1. Write TCP server program.

2. Get the http request message data sent by the browser.

3. Read the fixed page data, assemble the page data into HTTP response message data and send it to the browser.

4. After the HTTP response message data is sent, close the socket that serves the client.

Example

# time: 2021-10-21 20:38import socket if _ _ name__ ='_ _ main__': # create tcp server socket tcp_server_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # set port number reuse, program exit port immediately release tcp_server_socket.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR) True) # bind port number tcp_server_socket.bind ((", 8080)) # set to listen tcp_server_socket.listen (128C) while True: # wait to accept the connection request new_socket from the client, ip_port = tcp_server_socket.accept () # the code is executed so far Indicates that the connection was established successfully recv_client_data = new_socket.recv (4096) # Decoding binary data recv_client_content = recv_client_data.decode) print (recv_client_content) # response line response_line = "HTTP/1.1 200 OK\ r\ n" # response header Response_header = "Server: py1.0\ r\ n" # Responder response_body = "Hello Guys! "# splice response message response_data = (response_line + response_header +"\ r\ n "+ response_body). Encode () # send data new_socket.send (response_data) # close the service and client socket new_socket.close () to this I believe you have a deeper understanding of "python how to achieve static web server", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report