In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use python to create the simplest server. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
This article uses socket to implement
Access using python
Next, let's start to explain the implementation method.
Import socket
# 1. Create sockets and set server release
Web_server= socket.socket (socket.AF_INET, socket.SOCK_STREAM,socket.IPPROTO_TCP)
.web _ server.setsockopt (socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# 2. Bind port
Web_server.bind ("127.0.0.1", 26332))
# 3. Monitoring socket
Web_serve.listen (128)
The first step: we first create a socket, the first line, there are three parameters of the socket, here is not specific, just copy the code over it. The second line involves the TCP3 handshake and 4 waves, as long as you know that when our server initiates to close the client connection, or after an unexpected interruption, the port will not be occupied.
Step 2: bind the port, why bind the port, because only if you bind the port can others know how to find you.
If it is running locally, bind 127.0.0.1, and the port number can be entered at will.
If you are on a remote server, enter the IP address of the remote server. At the same time, the security rules are open to the corresponding ports.
Step 3: after we bind the port, we have to listen to the port. If we do not listen to the port, we will not know that someone is connected to you.
While True:
Web_client,client_ip=web_serve . Accept ()
Step 4: when we bind the port, we start waiting for the client to connect. If the client connects, it will proceed to the next step, and if there is no connection, it will block at this step.
Import multiprocessing
Process=multiprocessing.Process (target=web_requests,args= (web_client,))
Process.start ()
Web_client.close () Zhengzhou Gynecology Hospital http://www.120kdfk.com/
Step 5: when we receive the client connection, we have to create a process for the client to serve it, because the process is copying a web_client, so we can turn off the web_client that the program points to (the process is closed by the process) and cycle for the next client connection.
Def web_requests (web_client):
Web_requests=web_client.recv (1024) decode ("utf-8")
Define a process function, and then receive the data sent by the client. In web_client.recv (1024), recv (1024) receives the data sent by the client, and .decode ("utf-8") decodes the data sent by the client.
Import re
Web_request=web_requests.splitlines () [0] # convert the received data into a list, and take out the first line of parameters: GET / sroomkwang hellobook word HTTP/1.1
Request_kw=re.findall ("kw= (. *?) .HTTP", web_request) [0] # fetch the received parameters
When we climb Baidu, we often use wd=,&pn=, so here, we also use the method of passing parameters kw=, to let the server receive our request and return the passed parameter values. When we receive the request, we will start to return data.
Response_head= "HTTP/1.1 200 OK\ r\ n\ r\ n" .encode ("utf8") # returns head
Response_body=request_kw.encode ("utf8") # returns body
The returned data is divided into head and body. It is important to note here that in the browser, the returned head is not displayed on the page. You can check the returned head through f12. In python, we use requests to get the content, and the head section is not returned by default.
Body is the body that we return. It can be url or file, but be sure to switch to binary.
Web_client.send (response_head)
Web_client.send (response_body)
Then we use the send function to return the client head and body, at which point the client can receive the data we sent.
Be sure to remember that web_client.recv () receives the client request data, web_client.send () returns the data to the client, and don't forget to encode and decode it.
Web_client.close ()
After returning the client data, we close the connection in the process.
The above is all the code, you can copy all the code and run it directly. Here is a question for you. If you set up a password that needs to be verified, if the password passed in by the client is correct, the data will be returned. If it is incorrect, the password will be returned.
After reading the above, do you have any further understanding of how to create the simplest server with python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.