SocketServer模块为简化网络编程,对底层socket进行了封装。在3.0之后,模块名称改成了小写socketserver SocketServer介绍
There are four basic server classes: TCPServer uses the Internet TCP protocol, which provides for continuous streams of data between the client and server. UDPServer uses datagrams, which are discrete packets of information that may arrive out of order or be lost while in transit. The more infrequently used UnixStreamServer and UnixDatagramServer classes are similar, but use Unix domain sockets; they’re not available on non-Unix platforms。
主要有TCPServer、UDPServer、UnixStreamServer、UnixDatagramServer,类关系如下,主要用前2个Server,分别服务于TCP、UDP服务
+---------------+
| BaseServer |
+---------------+
|
v
+--------------+ +-------------------------+
| TCPServer |------->| UnixStreamServer |
+--------------+ +-------------------------+
|
v
+---------------+ +---------------------------+
| UDPServer |------->| UnixDatagramServer |
+---------------+ +---------------------------+
以上服务器都应该是异步处理,SocketServer提供了ForkingMixIn 和 ThreadingMixIn类支持异步处理
These four classes process requests synchronously; each request must be completed before the next request can be started. This isn’t suitable if each request takes a long time to complete, because it requires a lot of computation, or because it returns a lot of data which the client is slow to process. The solution is to create a separate process or thread to handle each request; the ForkingMixIn and ThreadingMixIn mix-in classes can be used to support asynchronous behaviour. Server创建
创建server服务需要下面3步
Creating a server requires several steps.
First, you must create a request handler class by subclassing the BaseRequestHandler class and overriding its handle() method; this method will process incoming requests.
Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class.
Finally, call the handle_request() or serve_forever() method of the server object to process one or many requests.
BaseRequestHandler有2个子类,分别对应于不同的类型的Socket类型
+---------------------------+ +-----------------------------------+
| BaseRequestHandler |------->| DatagramRequestHandler |
+---------------------------+ +---------------------------------+
|
v
+----------------------------------+
| StreamRequestHandler |
+----------------------------------+ SocketServer TCP例子
Server:
import SocketServer
class TCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
server = SocketServer.TCPServer((HOST, PORT), TCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
Client:
HOST, PORT = "localhost", 9999
#data = " ".join(sys.argv[1:])
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
data = "hello world\n"
try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(data)
# Receive data from the server and shut down
received = sock.recv(1024)
finally:
sock.close()
print "Sent: {}".format(data)
print "Received: {}".format(received)