1. Socketserver Framework
In Python, Socketserver is an integrated module that has the following features:
- You can create TCP and UDP servers by using the Socketserver framework.
- In the background for you to do a good job of every basic step.
- You can manually create a server to spoof the client and analyze its behavior.
2. Use of the Socketserver module
- Must be a subclass of Baserequesthandler
- overriding the handle () function
- Call handle_request or Serve_forever to handle the client program.
- For the TCP server,
- Self.request is the client socket
- Self.client_address is the client's detailed address
3. Python Code
Service side:
#!/usr/bin/env python# _*_ Coding=utf-8 _*_ImportSocketserver class sockhandler(socketserver.baserequesthandler): def handle(self): Print "received a connection to the client:", self.client_address data =' Start ' whileLen (data): data = SELF.REQUEST.RECV (2048) Self.request.send ("return:"+ data)Print "Client off ..."SERADDR = ("0.0.0.0",8888)Print "Waiting for connection ..."Server = Socketserver.tcpserver (seraddr,sockhandler) server.serve_forever ()
The client tests with the NC command:
$ nc 192.168.1.95 8888
How are you doing
Back to: Hello
Okay
return: Okay
Python socketserver Framework Programming