普通套接字方法(socket套接字)
1 from socket import socket 2 server = socket() 4 server.bind(('', 9999)) 5 server.listen(10) 6 7 conn, addr = server.accept() 8 recv_date = conn.recv(1024) 9 if recv_date: 10 print(recv_date) 11 conn.send(recv_date) 12 else: 13 conn.close()
非阻塞套接字方法(非阻塞套接字)
1 from socket import socket 2 server = socket() 3 server.setblocking(False) 4 server.bind(('', 9999)) 5 server.listen(100)
6 all_client = [] 7 while True: 8 try: 9 conn, addr = server.accept() 10 conn.setblocking(False) 11 all_client.append(conn) 12 except BlockingIOError: 13 pass 14 15 for conn in all_client: 16 try: 17 recv_date = conn.recv(1024) 18 if recv_date: 19 print(recv_date) 20 conn.send(recv_date) 21 else: 22 conn.close() 23 all_client.remove(conn) 24 except BlockingIOError: 25 pass
IO多路复用epoll方法(IO多路复用epollselector实现服务器)
1 import socket 2 import selectors 3 epoll_selector = selectors.EpollSelector() 4 server = socket.socket() 5 server.bind(('', 9980)) 6 server.listen(1000) 7 8 def recv(conn): 9 recv_date= conn.recv(1024) 10 if recv_date: 11 print(recv_date.decode()) 12 conn.send(recv_date) 13 else: 14 epoll_selector.unregister(conn) 15 conn.close() 16 17 def accept(server1): 18 conn, addr = server1.accept() 19 epoll_selector.register(conn, selectors.EVENT_READ, recv) 20 21 epoll_selector.register(server, selectors.EVENT_READ, accept) 22 23 while True: 24 events = epoll_selector.select() 25 for key, mask in events: 26 callable = key.data 27 sock = key.fileobj 28 callable(sock)
多进程方法(详解)
1 import multiprocessing 2 from socket import socket 3 server = socket() 4 server.bind(('', 9999)) 5 server.listen(100) 6 7 def func(conn): 8 while True: 9 recv_date = conn.recv(1024) 10 if recv_date: 11 print(recv_date) 12 conn.send(recv_date) 13 else: 14 conn.close() 15 16 while True: 17 conn, addr = server.accept() 18 m = multiprocessing.Process(target=func, args=(conn, )) 19 m.start()
线程方法
1 from socket import * 2 import threading 3 4 server = socket() 5 server.bind(('', 9999)) 6 server.listen(100) 7 8 def func(conn): 9 while True: 10 recv_date = conn.recv(1024) 11 if recv_date: 12 print(recv_date) 13 conn.send(recv_date) 14 else: 15 conn.close() 16 17 while True: 18 conn, addr = server.accept() 19 T = threading.Thread(target=func, args=(conn, )) 20 T.start()
进程池/线程池方法(方法介绍)
from socket import socket from multiprocessing import Pool, cpu_count from multiprocessing.pool import ThreadPool def process_accept(server): thread_pool = ThreadPool(n) while True: conn, addr = server.accept() thread_pool.apply_async(thread_recv, args=(conn, )) def thread_recv(conn): while True: recv_date = conn.recv(1024).decode() if recv_date: print(recv_date) conn.send(recv_date.encode()) else: conn.close() break server = socket() server.bind(('', 8899)) server.listen(200) n = cpu_count() pool = Pool(n) for i in range(n): pool.apply_async(process_accept, args=(server, )) pool.close() pool.join()
gevent协程实现
from gevent import monkey; monkey.patch_socket() from socket import socket import gevent server = socket() server.bind(('', 7788)) server.listen(1000) def recv(conn): while True: recv_date = conn.recv(1024).decode() if recv_date: print(recv_date) conn.send(recv_date.encode()) else: conn.close() while True: conn, addr = server.accept() gevent.spawn(recv, conn)