socket
一、Socket介绍
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。
socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求。
socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】【关闭】模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)
socket和file的区别:
-
- file模块是针对某个指定文件进行【打开】【读写】【关闭】
- socket模块是针对 服务器端 和 客户端Socket 进行【打开】【读写】【关闭】
1 #! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket 5 6 ip_port = ('127.0.0.1', 9998) 7 8 sk = socket.socket() # 生成一个socket句柄,默认tcp/ip 9 sk.bind(ip_port) # 绑定IP和端口 10 sk.listen(5) # 最多允许5个连接 11 12 while True: 13 print('server waiting...') 14 conn, address = sk.accept() # 等待连接进来,如果没有就一直等着(阻塞状态) 15 # conn是客户端实例 16 17 client_data = conn.recv(1024) 18 print(str(client_data, 'utf8')) 19 conn.sendall(bytes("不要回答,不要回答,不要回答", 'utf8')) 20 21 conn.close()
1 #! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket 5 ip_port = ('127.0.0.1', 9998) 6 7 sk = socket.socket() # 生成一个socket句柄,默认tcp/ip 8 sk.connect(ip_port) # 连接服务器 9 10 sk.sendall(bytes("请求占领地球", 'utf8')) 11 12 server_reply = sk.recv(1024) # 接收1024个字节 13 print(str(server_reply, 'utf8')) 14 15 sk.close()
基于socket的web服务器
网络通信的三要素是IP地址、端口号和传输协议,IP地址用来标识网络上一台独立的主机,端口号用来标识进程的逻辑地址,传输协议定义通讯的规则。我们可以利用ip地址+协议+端口号唯一标示网络中的一个进程。能够唯一标示网络中的进程后,它们就可以利用socket进行通信了。Web应用的本质其实就是一个socket服务端,用户的浏览器就是一个socket客户端。
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket 5 6 def handle_request(client): 7 buf = client.recv(1024) 8 client.send(bytes("HTTP/1.1 200 OK\r\n\r\n", 'utf8')) 9 client.send(bytes("Hello, World", 'utf8')) 10 11 def main(): 12 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 13 sock.bind(('localhost', 8080)) 14 sock.listen(5) 15 16 while True: 17 print('server waiting...') 18 connection, address = sock.accept() 19 handle_request(connection) 20 connection.close() 21 22 if __name__ == '__main__': 23 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 ,则系统就会根据地址格式和套接类别,自动选择一个合适的协议
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()
套接字的文件描述符
二、socketserver模块
socketserver内部使用 IO多路复用 以及 “多线程” 或 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端。即:每个客户端请求连接到服务器时,Socket服务端都会在服务器是创建一个“线程”或者“进程” 专门负责处理当前客户端的所有请求。
There are five classes in an inheritance diagram, four of which represent synchronous servers of four types: +------------+ | BaseServer | +------------+ | v +-----------+ +------------------+ | TCPServer |------->| UnixStreamServer | +-----------+ +------------------+ | v +-----------+ +--------------------+ | UDPServer |------->| UnixDatagramServer | +-----------+ +--------------------+ Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer — the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both Unix server classes. class socketserver.ForkingMixIn class socketserver.ThreadingMixIn Forking and threading versions of each type of server can be created using these mix-in classes. For instance, ThreadingUDPServer is created as follows: class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass The mix-in class comes first, since it overrides a method defined in UDPServer. Setting the various attributes also changes the behavior of the underlying server mechanism. class socketserver.ForkingTCPServer class socketserver.ForkingUDPServer class socketserver.ThreadingTCPServer class socketserver.ThreadingUDPServer These classes are pre-defined using the mix-in classes.
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socketserver 5 class MyTCPHandler(socketserver.BaseRequestHandler): 6 def setup(self): 7 print("Building secure connection chanel...") 8 def handle(self): 9 print("New Conn:", self.client_address) 10 while True: 11 12 data = self.request.recv(1024) 13 if not data: 14 break 15 print("Client Says:", data.decode()) 16 self.request.send(data) 17 def finish(self): 18 print("client conn is done...") 19 if __name__ == '__main__': 20 HOST, PORT = "localhost", 50007 21 # 把刚才写的类当作一个参数传给ThreadingTCPServer这个类,下面的代码就创建了一个多线程socket server 22 server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) 23 # 启动这个server,这个server会一直运行,除非按ctrl-C停止 24 server.serve_forever()
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import socket 4 ip_port = ('127.0.0.1', 50007) 5 6 sk = socket.socket() 7 sk.connect(ip_port) 8 while True: 9 msg = input(">>:").strip() 10 sk.sendall(bytes(msg, 'utf8')) 11 server_reply = sk.recv(1024) 12 print("Server Reply:", str(server_reply, 'utf8')) 13 sk.close()
ThreadTCPServer
ThreadingTCPServer实现的Soket服务器内部会为每个client创建一个 “线程”,该线程用来和客户端进行交互。
1、ThreadTCPServer基础
使用ThreadTCPServer:
- 创建一个继承自 SocketServer.BaseRequestHandler 的类
- 类中必须定义一个名称为 handle 的方法
- 启动ThreadingTCPServer
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): 2 pass
BaseRequestHandle源码
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
参考:
http://www.cnblogs.com/wupeiqi/articles/5040823.html