How to configure the Python socket Service

Source: Internet
Author: User

The Python socket service requires constant learning, because extensive applications require constant learning. The following is a detailed introduction. Hope to help you. First, you must understand that not all socket services require high performance.

If high performance is required, C/C ++ is used in the IOCP or EPoll mode, and the API is used for writing directly. It is more appropriate to use the proactor encapsulation of ACE. But when performance is not a major problem, using the Python socket service and enjoying high development efficiency will be a pleasure. The following is an echo service for every thread/connect completed by using the Python socket service.

Often, when writing a piece of Python socket service code, I will first open the book "Python Cookbook" O 'Reilly) to see if there is any need, which is also a way to ensure efficiency ), the following code is taken from this book.

 
 
  1. import SocketServer  
  2. class MyHandler(SocketServer.BaseRequestHandler):  
  3. def handle(self):  
  4. while 1:  
  5. dataReceived = self.request.recv(1024)  
  6. if not dataReceived: break  
  7. self.request.send(dataReceived)  
  8. myServer = SocketServer.ThreadingTCPServer(('',8881), MyHandler)  
  9. myServer.serve_forever( ) 

It is not very easy and pleasant to complete the work with only a few lines of code. Note that this is not a utility, but a simple example. However, this example shows the direction. Below I will list some tips for completing a real server. However, before that, it was a good idea to use several lines of code to complete a test client:

 
 
  1. import socket  
  2. remote_host = '127.0.0.1' 
  3. remote_port = 9919 
  4. send_buf = open('binary.txt', 'rb').read()   
  5. #send_bufsend_buf = send_buf.replace('\x0D\x0A', '')   
  6. ock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
  7. sock.connect((remote_host, remote_port))  
  8. sock.send(send_buf)  
  9. response_data = sock.recv(1024)  
  10. print response_data  
  11. sock.close( ) 

Looking at the code above, do you feel that the development efficiency is not very high ^ _ ^? Let's go to the topic below

Now, let's solve the first problem. MyHander inherits from SocketServer. BaseRequestHandler, but this module is not detailed in this document. Why not? I think this class is very simple. Open the SocketServer. py file in the Lib directory. Let's look at the Code:

 
 
  1. class BaseRequestHandler:  
  2. def __init__(self, request, client_address, server):  
  3. self.request = request  
  4. self.client_address = client_address  
  5. self.server = server  
  6. try:  
  7. self.setup()  
  8. self.handle()  
  9. self.finish()  
  10. finally:  
  11. sys.exc_traceback = None # Help garbage collection  
  12. def setup(self):  
  13. pass  
  14. def handle(self):  
  15. pass  
  16. def finish(self):  
  17. pass  

The above is a detailed introduction to the Python socket service. Classes implement a simple template mode, defining setup, handle, and finish to reload successors, mode Method _ init _ defines the call sequence of the three methods to ensure the operation of the three methods at the same time. Obviously, if we want to close the connection at exit, it is natural to redefine finish.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.