Python学习之路--Socket
Socket
socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求。
socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】【关闭】模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)
socket和file的区别:
- file模块是针对某个指定文件进行【打开】【读写】【关闭】
- socket模块是针对 服务器端 和 客户端Socket 进行【打开】【读写】【关闭】
#服务端
1 import socket 2 ip_port = ('127.0.0.2',99) 3 4 sk = socket.socket() 5 sk.bind(ip_port) 6 sk.listen(5) 7 8 while True: 9 print('server waiting...') 10 conn,addr = sk.accept() 11 12 client_data = conn.recv(1024) 13 print(str(client_data,'utf8')) 14 conn.sendall(bytes('不要回答,不要回答,不要回答','utf8')) 15 conn.close()
#客户端
1 import socket 2 ip_port = ('127.0.0.2',99) 3 4 sk = socket.socket() 5 sk.connect(ip_port) 6 7 sk.sendall(bytes('请求占领地球','utf8')) 8 server_reply = sk.recv(1024) 9 print(str(server_reply,'utf8')) 10 while True: 11 user_input = input(">>:").strip() 12 sk.send(bytes(user_input,'utf8')) 13 server_reply = sk.recv(1024) 14 print(str(server_reply,'utf8')) 15 sk.close()
WEB服务应用:
#!/usr/bin/env python #coding:utf-8 import socket def handle_request(client): buf = client.recv(1024) client.send("HTTP/1.1 200 OK\r\n\r\n") client.send("Hello, World") def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost',8080)) sock.listen(5) while True: connection, address = sock.accept() handle_request(connection) connection.close() if __name__ == '__main__': main()
更多功能
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0)
参数一:地址簇
socket.AF_INET IPv4(默认)
socket.AF_INET6 IPv6
socket.AF_UNIX 只能够用于单一的Unix系统进程间通信
参数二:类型
socket.SOCK_STREAM 流式socket , for TCP (默认)
socket.SOCK_DGRAM 数据报式socket , for UDP
socket.SOCK_RAW 原始套接字,普通的套接字无法处理ICMP、IGMP等网络报文,而SOCK_RAW可以;其次,SOCK_RAW也可以处理特殊的IPv4报文;此外,利用原始套接字,可以通过IP_HDRINCL套接字选项由用户构造IP头。
socket.SOCK_RDM 是一种可靠的UDP形式,即保证交付数据报但不保证顺序。SOCK_RAM用来提供对原始协议的低级访问,在需要执行某些特殊操作时使用,如发送ICMP报文。SOCK_RAM通常仅限于高级用户或管理员运行的程序使用。
socket.SOCK_SEQPACKET 可靠的连续数据包服务
参数三:协议
0 (默认)与特定的地址家族相关的协议,如果是 0 ,则系统就会根据地址格式和套接类别,自动选择一个合适的协议
UDP Demo
sk.bind(address)
s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下,以元组(host,port)的形式表示地址。
sk.listen(backlog)
开始监听传入连接。backlog指定在拒绝连接之前,可以挂起的最大连接数量。
backlog等于5,表示内核已经接到了连接请求,但服务器还没有调用accept进行处理的连接个数最大为5
这个值不能无限大,因为要在内核中维护连接队列
sk.setblocking(bool)
是否阻塞(默认True),如果设置False,那么accept和recv时一旦无数据,则报错。
sk.accept()
接受连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。address是连接客户端的地址。
接收TCP 客户的连接(阻塞式)等待连接的到来
sk.connect(address)
连接到address处的套接字。一般,address的格式为元组(hostname,port),如果连接出错,返回socket.error错误。
sk.connect_ex(address)
同上,只不过会有返回值,连接成功时返回 0 ,连接失败时候返回编码,例如:10061
sk.close()
关闭套接字
sk.recv(bufsize[,flag])
接受套接字的数据。数据以字符串形式返回,bufsize指定最多可以接收的数量。flag提供有关消息的其他信息,通常可以忽略。
sk.recvfrom(bufsize[.flag])
与recv()类似,但返回值是(data,address)。其中data是包含接收数据的字符串,address是发送数据的套接字地址。
sk.send(string[,flag])
将string中的数据发送到连接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。即:可能未将指定内容全部发送。
sk.sendall(string[,flag])
将string中的数据发送到连接的套接字,但在返回之前会尝试发送所有数据。成功返回None,失败则抛出异常。
内部通过递归调用send,将所有内容发送出去。
sk.sendto(string[,flag],address)
将数据发送到套接字,address是形式为(ipaddr,port)的元组,指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。
sk.settimeout(timeout)
设置套接字操作的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。一般,超时期应该在刚创建套接字时设置,因为它们可能用于连接的操作(如 client 连接最多等待5s )
sk.getpeername()
返回连接套接字的远程地址。返回值通常是元组(ipaddr,port)。
sk.getsockname()
返回套接字自己的地址。通常是一个元组(ipaddr,port)
sk.fileno()
套接字的文件描述符
实例:智能机器人
1 #服务端 2 #!/usr/bin/env python 3 # -*- coding:utf-8 -*- 4 5 6 import socket 7 8 ip_port = ('127.0.0.1',8888) 9 sk = socket.socket() 10 sk.bind(ip_port) 11 sk.listen(5) 12 13 while True: 14 conn,address = sk.accept() 15 conn.sendall('欢迎致电 10086,请输入1xxx,0转人工服务.') 16 Flag = True 17 while Flag: 18 data = conn.recv(1024) 19 if data == 'exit': 20 Flag = False 21 elif data == '0': 22 conn.sendall('通过可能会被录音.balabala一大推') 23 else: 24 conn.sendall('请重新输入.') 25 conn.close()
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket 5 6 7 ip_port = ('127.0.0.1',8005) 8 sk = socket.socket() 9 sk.connect(ip_port) 10 sk.settimeout(5) 11 12 while True: 13 data = sk.recv(1024) 14 print 'receive:',data 15 inp = raw_input('please input:') 16 sk.sendall(inp) 17 if inp == 'exit': 18 break 19 20 sk.close()
IO多路复用
I/O多路复用指:通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。
Linux
Linux中的 select,poll,epoll 都是IO多路复用的机制。
select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被内核修改标志位,使得进程可以获得这些文件描述符从而进行后续的读写操作。 select目前几乎在所有的平台上支持,其良好跨平台支持也是它的一个优点,事实上从现在看来,这也是它所剩不多的优点之一。 select的一个缺点在于单个进程能够监视的文件描述符的数量存在最大限制,在Linux上一般为1024,不过可以通过修改宏定义甚至重新编译内核的方式提升这一限制。 另外,select()所维护的存储大量文件描述符的数据结构,随着文件描述符数量的增大,其复制的开销也线性增长。同时,由于网络响应时间的延迟使得大量TCP连接处于非活跃状态,但调用select()会对所有socket进行一次线性扫描,所以这也浪费了一定的开销。 poll poll在1986年诞生于System V Release 3,它和select在本质上没有多大差别,但是poll没有最大文件描述符数量的限制。 poll和select同样存在一个缺点就是,包含大量文件描述符的数组被整体复制于用户态和内核的地址空间之间,而不论这些文件描述符是否就绪,它的开销随着文件描述符数量的增加而线性增大。 另外,select()和poll()将就绪的文件描述符告诉进程后,如果进程没有对其进行IO操作,那么下次调用select()和poll()的时候将再次报告这些文件描述符,所以它们一般不会丢失就绪的消息,这种方式称为水平触发(Level Triggered)。 epoll 直到Linux2.6才出现了由内核直接支持的实现方法,那就是epoll,它几乎具备了之前所说的一切优点,被公认为Linux2.6下性能最好的多路I/O就绪通知方法。 epoll可以同时支持水平触发和边缘触发(Edge Triggered,只告诉进程哪些文件描述符刚刚变为就绪状态,它只说一遍,如果我们没有采取行动,那么它将不会再次告知,这种方式称为边缘触发),理论上边缘触发的性能要更高一些,但是代码实现相当复杂。 epoll同样只告知那些就绪的文件描述符,而且当我们调用epoll_wait()获得就绪文件描述符时,返回的不是实际的描述符,而是一个代表就绪描述符数量的值,你只需要去epoll指定的一个数组中依次取得相应数量的文件描述符即可,这里也使用了内存映射(mmap)技术,这样便彻底省掉了这些文件描述符在系统调用时复制的开销。 另一个本质的改进在于epoll采用基于事件的就绪通知方式。在select/poll中,进程只有在调用一定的方法后,内核才对所有监视的文件描述符进行扫描,而epoll事先通过epoll_ctl()来注册一个文件描述符,一旦基于某个文件描述符就绪时,内核会采用类似callback的回调机制,迅速激活这个文件描述符,当进程调用epoll_wait()时便得到通知。
Python
Python中有一个select模块,其中提供了:select、poll、epoll三个方法,分别调用系统的 select,poll,epoll 从而实现IO多路复用。
注意:网络操作、文件操作、终端操作等均属于IO操作,对于windows只支持Socket操作,其他系统支持其他IO操作,但是无法检测 普通文件操作 自动上次读取是否已经变化。
对于select方法:
句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超时时间) 参数: 可接受四个参数(前三个必须) 返回值:三个列表 select方法用来监视文件句柄,如果句柄发生变化,则获取该句柄。 1、当 参数1 序列中的句柄发生可读时(accetp和read),则获取发生变化的句柄并添加到 返回值1 序列中 2、当 参数2 序列中含有句柄时,则将该序列中所有的句柄添加到 返回值2 序列中 3、当 参数3 序列中的句柄发生错误时,则将该发生错误的句柄添加到 返回值3 序列中 4、当 超时时间 未设置,则select会一直阻塞,直到监听的句柄发生变化 当 超时时间 = 1时,那么如果监听的句柄均无任何变化,则select会阻塞 1 秒,之后返回三个空列表,如果监听的句柄有变化,则直接执行。
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import select 5 import threading 6 import sys 7 8 while True: 9 readable, writeable, error = select.select([sys.stdin,],[],[],1) 10 if sys.stdin in readable: 11 print 'select get stdin',sys.stdin.readline()
1 #利用select实现伪同时处理多个Socket客户端请求:服务端 2 #!/usr/bin/env python 3 # -*- coding:utf-8 -*- 4 5 import socket 6 import select 7 8 sk1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 sk1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 10 sk1.bind(('127.0.0.1',8002)) 11 sk1.listen(5) 12 #sk1.setblocking(0) 13 14 inputs = [sk1,] 15 print("打印出-->:",sk1) 16 while True: 17 readable_list, writeable_list, error_list = select.select(inputs, [], inputs, 1) 18 print('readable_list',readable_list) 19 print('writeable_list',writeable_list) 20 for r in readable_list: 21 print(r) 22 # 当客户端第一次连接服务端时 23 if sk1 == r: 24 print('accept start ... ') 25 request, address = r.accept() 26 print('what',request,address) 27 request.setblocking(0) 28 inputs.append(request) 29 # 当客户端连接上服务端之后,再次发送数据时 30 else: 31 received = r.recv(1024) 32 # 当正常接收客户端发送的数据时 33 if received: 34 print('received data:', received) 35 # 当客户端关闭程序时 36 else: 37 inputs.remove(r) 38 print('no data') 39 40 sk1.close()
#利用select实现伪同时处理多个Socket客户端请求:客户端 #!/usr/bin/env python # -*- coding:utf-8 -*- import socket ip_port = ('127.0.0.1',8002) sk = socket.socket() sk.connect(ip_port) while True: inp = raw_input('please input:') sk.sendall(inp) sk.close()
此处的Socket服务端相比与原生的Socket,他支持当某一个请求不再发送数据时,服务器端不会等待而是可以去处理其他请求的数据。但是,如果每个请求的耗时比较长时,select版本的服务器端也无法完成同时操作。
SocketServer模块
SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端。即:每个客户端请求连接到服务器时,Socket服务端都会在服务器是创建一个“线程”或者“进程” 专门负责处理当前客户端的所有请求。
ThreadingTCPServer
ThreadingTCPServer实现的Soket服务器内部会为每个client创建一个 “线程”,该线程用来和客户端进行交互。
1、ThreadingTCPServer基础
使用ThreadingTCPServer:
- 创建一个继承自 SocketServer.BaseRequestHandler 的类
- 类中必须定义一个名称为 handle 的方法
- 启动ThreadingTCPServer
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import SocketServer 4 5 class MyServer(SocketServer.BaseRequestHandler): 6 7 def handle(self): 8 # print self.request,self.client_address,self.server 9 conn = self.request 10 conn.sendall(bytes('欢迎致电 10086,请输入1xxx,0转人工服务.','utf-8')) 11 Flag = True 12 while Flag: 13 data = conn.recv(1024) 14 if data == 'exit': 15 Flag = False 16 elif data == '0': 17 conn.sendall(bytes('通过可能会被录音.balabala一大推','utf-8')) 18 else: 19 conn.sendall(bytes('请重新输入.','utf-8')) 20 21 22 if __name__ == '__main__': 23 server = SocketServer.ThreadingTCPServer(('127.0.0.1',8009),MyServer) 24 server.serve_forever()
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket 5 6 7 ip_port = ('127.0.0.1',8009) 8 sk = socket.socket() 9 sk.connect(ip_port) 10 sk.settimeout(5) 11 12 while True: 13 data = sk.recv(1024) 14 print 'receive:',data 15 inp = input('please input:') 16 sk.sendall(bytes(inp,'utf-8')) 17 if inp == 'exit': 18 break 19 20 sk.close()
2、ThreadingTCPServer源码剖析
ThreadingTCPServer的类图关系如下:
内部调用流程为:
- 启动服务端程序
- 执行 TCPServer.__init__ 方法,创建服务端Socket对象并绑定 IP 和 端口
- 执行 BaseServer.__init__ 方法,将自定义的继承自SocketServer.BaseRequestHandler 的类 MyRequestHandle赋值给 self.RequestHandlerClass
- 执行 BaseServer.server_forever 方法,While 循环一直监听是否有客户端请求到达 ...
- 当客户端连接到达服务器
- 执行 ThreadingMixIn.process_request 方法,创建一个 “线程” 用来处理请求
- 执行 ThreadingMixIn.process_request_thread 方法
- 执行 BaseServer.finish_request 方法,执行 self.RequestHandlerClass() 即:执行 自定义 MyRequestHandler 的构造方法(自动调用基类BaseRequestHandler的构造方法,在该构造方法中又会调用 MyRequestHandler的handle方法)
ThreadingTCPServer相关源码:
1 class BaseServer: 2 3 """Base class for server classes. 4 5 Methods for the caller: 6 7 - __init__(server_address, RequestHandlerClass) 8 - serve_forever(poll_interval=0.5) 9 - shutdown() 10 - handle_request() # if you do not use serve_forever() 11 - fileno() -> int # for select() 12 13 Methods that may be overridden: 14 15 - server_bind() 16 - server_activate() 17 - get_request() -> request, client_address 18 - handle_timeout() 19 - verify_request(request, client_address) 20 - server_close() 21 - process_request(request, client_address) 22 - shutdown_request(request) 23 - close_request(request) 24 - handle_error() 25 26 Methods for derived classes: 27 28 - finish_request(request, client_address) 29 30 Class variables that may be overridden by derived classes or 31 instances: 32 33 - timeout 34 - address_family 35 - socket_type 36 - allow_reuse_address 37 38 Instance variables: 39 40 - RequestHandlerClass 41 - socket 42 43 """ 44 45 timeout = None 46 47 def __init__(self, server_address, RequestHandlerClass): 48 """Constructor. May be extended, do not override.""" 49 self.server_address = server_address 50 self.RequestHandlerClass = RequestHandlerClass 51 self.__is_shut_down = threading.Event() 52 self.__shutdown_request = False 53 54 def server_activate(self): 55 """Called by constructor to activate the server. 56 57 May be overridden. 58 59 """ 60 pass 61 62 def serve_forever(self, poll_interval=0.5): 63 """Handle one request at a time until shutdown. 64 65 Polls for shutdown every poll_interval seconds. Ignores 66 self.timeout. If you need to do periodic tasks, do them in 67 another thread. 68 """ 69 self.__is_shut_down.clear() 70 try: 71 while not self.__shutdown_request: 72 # XXX: Consider using another file descriptor or 73 # connecting to the socket to wake this up instead of 74 # polling. Polling reduces our responsiveness to a 75 # shutdown request and wastes cpu at all other times. 76 r, w, e = _eintr_retry(select.select, [self], [], [], 77 poll_interval) 78 if self in r: 79 self._handle_request_noblock() 80 finally: 81 self.__shutdown_request = False 82 self.__is_shut_down.set() 83 84 def shutdown(self): 85 """Stops the serve_forever loop. 86 87 Blocks until the loop has finished. This must be called while 88 serve_forever() is running in another thread, or it will 89 deadlock. 90 """ 91 self.__shutdown_request = True 92 self.__is_shut_down.wait() 93 94 # The distinction between handling, getting, processing and 95 # finishing a request is fairly arbitrary. Remember: 96 # 97 # - handle_request() is the top-level call. It calls 98 # select, get_request(), verify_request() and process_request() 99 # - get_request() is different for stream or datagram sockets 100 # - process_request() is the place that may fork a new process 101 # or create a new thread to finish the request 102 # - finish_request() instantiates the request handler class; 103 # this constructor will handle the request all by itself 104 105 def handle_request(self): 106 """Handle one request, possibly blocking. 107 108 Respects self.timeout. 109 """ 110 # Support people who used socket.settimeout() to escape 111 # handle_request before self.timeout was available. 112 timeout = self.socket.gettimeout() 113 if timeout is None: 114 timeout = self.timeout 115 elif self.timeout is not None: 116 timeout = min(timeout, self.timeout) 117 fd_sets = _eintr_retry(select.select, [self], [], [], timeout) 118 if not fd_sets[0]: 119 self.handle_timeout() 120 return 121 self._handle_request_noblock() 122 123 def _handle_request_noblock(self): 124 """Handle one request, without blocking. 125 126 I assume that select.select has returned that the socket is 127 readable before this function was called, so there should be 128 no risk of blocking in get_request(). 129 """ 130 try: 131 request, client_address = self.get_request() 132 except socket.error: 133 return 134 if self.verify_request(request, client_address): 135 try: 136 self.process_request(request, client_address) 137 except: 138 self.handle_error(request, client_address) 139 self.shutdown_request(request) 140 141 def handle_timeout(self): 142 """Called if no new request arrives within self.timeout. 143 144 Overridden by ForkingMixIn. 145 """ 146 pass 147 148 def verify_request(self, request, client_address): 149 """Verify the request. May be overridden. 150 151 Return True if we should proceed with this request. 152 153 """ 154 return True 155 156 def process_request(self, request, client_address): 157 """Call finish_request. 158 159 Overridden by ForkingMixIn and ThreadingMixIn. 160 161 """ 162 self.finish_request(request, client_address) 163 self.shutdown_request(request) 164 165 def server_close(self): 166 """Called to clean-up the server. 167 168 May be overridden. 169 170 """ 171 pass 172 173 def finish_request(self, request, client_address): 174 """Finish one request by instantiating RequestHandlerClass.""" 175 self.RequestHandlerClass(request, client_address, self) 176 177 def shutdown_request(self, request): 178 """Called to shutdown and close an individual request.""" 179 self.close_request(request) 180 181 def close_request(self, request): 182 """Called to clean up an individual request.""" 183 pass 184 185 def handle_error(self, request, client_address): 186 """Handle an error gracefully. May be overridden. 187 188 The default is to print a traceback and continue. 189 190 """ 191 print '-'*40 192 print 'Exception happened during processing of request from', 193 print client_address 194 import traceback 195 traceback.print_exc() # XXX But this goes to stderr! 196 print '-'*40
1 class TCPServer(BaseServer): 2 3 """Base class for various socket-based server classes. 4 5 Defaults to synchronous IP stream (i.e., TCP). 6 7 Methods for the caller: 8 9 - __init__(server_address, RequestHandlerClass, bind_and_activate=True) 10 - serve_forever(poll_interval=0.5) 11 - shutdown() 12 - handle_request() # if you don't use serve_forever() 13 - fileno() -> int # for select() 14 15 Methods that may be overridden: 16 17 - server_bind() 18 - server_activate() 19 - get_request() -> request, client_address 20 - handle_timeout() 21 - verify_request(request, client_address) 22 - process_request(request, client_address) 23 - shutdown_request(request) 24 - close_request(request) 25 - handle_error() 26 27 Methods for derived classes: 28 29 - finish_request(request, client_address) 30 31 Class variables that may be overridden by derived classes or 32 instances: 33 34 - timeout 35 - address_family 36 - socket_type 37 - request_queue_size (only for stream sockets) 38 - allow_reuse_address 39 40 Instance variables: 41 42 - server_address 43 - RequestHandlerClass 44 - socket 45 46 """ 47 48 address_family = socket.AF_INET 49 50 socket_type = socket.SOCK_STREAM 51 52 request_queue_size = 5 53 54 allow_reuse_address = False 55 56 def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): 57 """Constructor. May be extended, do not override.""" 58 BaseServer.__init__(self, server_address, RequestHandlerClass) 59 self.socket = socket.socket(self.address_family, 60 self.socket_type) 61 if bind_and_activate: 62 try: 63 self.server_bind() 64 self.server_activate() 65 except: 66 self.server_close() 67 raise 68 69 def server_bind(self): 70 """Called by constructor to bind the socket. 71 72 May be overridden. 73 74 """ 75 if self.allow_reuse_address: 76 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 77 self.socket.bind(self.server_address) 78 self.server_address = self.socket.getsockname() 79 80 def server_activate(self): 81 """Called by constructor to activate the server. 82 83 May be overridden. 84 85 """ 86 self.socket.listen(self.request_queue_size) 87 88 def server_close(self): 89 """Called to clean-up the server. 90 91 May be overridden. 92 93 """ 94 self.socket.close() 95 96 def fileno(self): 97 """Return socket file number. 98 99 Interface required by select(). 100 101 """ 102 return self.socket.fileno() 103 104 def get_request(self): 105 """Get the request and client address from the socket. 106 107 May be overridden. 108 109 """ 110 return self.socket.accept() 111 112 def shutdown_request(self, request): 113 """Called to shutdown and close an individual request.""" 114 try: 115 #explicitly shutdown. socket.close() merely releases 116 #the socket and waits for GC to perform the actual close. 117 request.shutdown(socket.SHUT_WR) 118 except socket.error: 119 pass #some platforms may raise ENOTCONN here 120 self.close_request(request) 121 122 def close_request(self, request): 123 """Called to clean up an individual request.""" 124 request.close()
1 class ThreadingMixIn: 2 """Mix-in class to handle each request in a new thread.""" 3 4 # Decides how threads will act upon termination of the 5 # main process 6 daemon_threads = False 7 8 def process_request_thread(self, request, client_address): 9 """Same as in BaseServer but as a thread. 10 11 In addition, exception handling is done here. 12 13 """ 14 try: 15 self.finish_request(request, client_address) 16 self.shutdown_request(request) 17 except: 18 self.handle_error(request, client_address) 19 self.shutdown_request(request) 20 21 def process_request(self, request, client_address): 22 """Start a new thread to process the request.""" 23 t = threading.Thread(target = self.process_request_thread, 24 args = (request, client_address)) 25 t.daemon = self.daemon_threads 26 t.start()
1 class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
RequestHandler相关源码
1 class BaseRequestHandler: 2 3 """Base class for request handler classes. 4 5 This class is instantiated for each request to be handled. The 6 constructor sets the instance variables request, client_address 7 and server, and then calls the handle() method. To implement a 8 specific service, all you need to do is to derive a class which 9 defines a handle() method. 10 11 The handle() method can find the request as self.request, the 12 client address as self.client_address, and the server (in case it 13 needs access to per-server information) as self.server. Since a 14 separate instance is created for each request, the handle() method 15 can define arbitrary other instance variariables. 16 17 """ 18 19 def __init__(self, request, client_address, server): 20 self.request = request 21 self.client_address = client_address 22 self.server = server 23 self.setup() 24 try: 25 self.handle() 26 finally: 27 self.finish() 28 29 def setup(self): 30 pass 31 32 def handle(self): 33 pass 34 35 def finish(self): 36 pass
实例:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import SocketServer 4 5 class MyServer(SocketServer.BaseRequestHandler): 6 7 def handle(self): 8 # print self.request,self.client_address,self.server 9 conn = self.request 10 conn.sendall('欢迎致电 10086,请输入1xxx,0转人工服务.') 11 Flag = True 12 while Flag: 13 data = conn.recv(1024) 14 if data == 'exit': 15 Flag = False 16 elif data == '0': 17 conn.sendall('通过可能会被录音.balabala一大推') 18 else: 19 conn.sendall('请重新输入.') 20 21 22 if __name__ == '__main__': 23 server = SocketServer.ThreadingTCPServer(('127.0.0.1',8009),MyServer) 24 server.serve_forever()
#!/usr/bin/env python # -*- coding:utf-8 -*- import socket ip_port = ('127.0.0.1',8009) sk = socket.socket() sk.connect(ip_port) sk.settimeout(5) while True: data = sk.recv(1024) print 'receive:',data inp = raw_input('please input:') sk.sendall(inp) if inp == 'exit': break sk.close()
源码精简:
1 import socket 2 import threading 3 import select 4 5 6 def process(request, client_address): 7 print request,client_address 8 conn = request 9 conn.sendall('欢迎致电 10086,请输入1xxx,0转人工服务.') 10 flag = True 11 while flag: 12 data = conn.recv(1024) 13 if data == 'exit': 14 flag = False 15 elif data == '0': 16 conn.sendall('通过可能会被录音.balabala一大推') 17 else: 18 conn.sendall('请重新输入.') 19 20 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 21 sk.bind(('127.0.0.1',8002)) 22 sk.listen(5) 23 24 while True: 25 r, w, e = select.select([sk,],[],[],1) 26 print 'looping' 27 if sk in r: 28 print 'get request' 29 request, client_address = sk.accept() 30 t = threading.Thread(target=process, args=(request, client_address)) 31 t.daemon = False 32 t.start() 33 34 sk.close()
如精简代码可以看出,SocketServer的ThreadingTCPServer之所以可以同时处理请求得益于 select 和Threading 两个东西,其实本质上就是在服务器端为每一个客户端创建一个线程,当前线程用来处理对应客户端的请求,所以,可以支持同时n个客户端链接(长连接)。