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.
- import SocketServer
- class MyHandler(SocketServer.BaseRequestHandler):
- def handle(self):
- while 1:
- dataReceived = self.request.recv(1024)
- if not dataReceived: break
- self.request.send(dataReceived)
- myServer = SocketServer.ThreadingTCPServer(('',8881), MyHandler)
- 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:
- import socket
- remote_host = '127.0.0.1'
- remote_port = 9919
- send_buf = open('binary.txt', 'rb').read()
- #send_bufsend_buf = send_buf.replace('\x0D\x0A', '')
- ock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.connect((remote_host, remote_port))
- sock.send(send_buf)
- response_data = sock.recv(1024)
- print response_data
- 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:
- class BaseRequestHandler:
- def __init__(self, request, client_address, server):
- self.request = request
- self.client_address = client_address
- self.server = server
- try:
- self.setup()
- self.handle()
- self.finish()
- finally:
- sys.exc_traceback = None # Help garbage collection
- def setup(self):
- pass
- def handle(self):
- pass
- def finish(self):
- 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.