First, based on the TCP protocol
Based on tcp socket, the key is two cycles, a cycle link, a communication cycle
socketserver module carved into two categories: server class (link to solve the problem) and request class (to solve the communication problems)
1.1 server class
1.2 request class
1.3 inheritance
1.4 server
import socketserver
class MyHandler(socketserver.BaseRequestHandler):
def handle(self):
# 通信循环
while True:
# print(self.client_address)
# print(self.request) #self.request=conn
try:
data = self.request.recv(1024)
if len(data) == 0: break
self.request.send(data.upper())
except ConnectionResetError:
break
if __name__ == '__main__':
s = socketserver.ThreadingTCPServer(('127.0.0.1', 8080), MyHandler, bind_and_activate=True)
s.serve_forever() # 代表连接循环
# 循环建立连接,每建立一个连接就会启动一个线程(服务员)+调用Myhanlder类产生一个对象,调用该对象下的handle方法,专门与刚刚建立好的连接做通信循环
1.5 Client
import socket
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.connect(('127.0.0.1', 8080)) # 指定服务端ip和端口
while True:
# msg=input('>>: ').strip() #msg=''
msg = 'client33333' # msg=''
if len(msg) == 0: continue
phone.send(msg.encode('utf-8'))
data = phone.recv(1024)
print(data)
phone.close()
1.6 Client 1
import socket
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.connect(('127.0.0.1', 8080)) # 指定服务端ip和端口
while True:
# msg=input('>>: ').strip() #msg=''
msg = 'client11111' # msg=''
if len(msg) == 0: continue
phone.send(msg.encode('utf-8'))
data = phone.recv(1024)
print(data)
phone.close()
Second, based on the UDP protocol
2.1 server
import socketserver
class MyHandler(socketserver.BaseRequestHandler):
def handle(self):
# 通信循环
print(self.client_address)
print(self.request)
data = self.request[0]
print('客户消息', data)
self.request[1].sendto(data.upper(), self.client_address)
if __name__ == '__main__':
s = socketserver.ThreadingUDPServer(('127.0.0.1', 8080), MyHandler)
s.serve_forever()
2.2 Client
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 数据报协议-》udp
while True:
# msg=input('>>: ').strip() #msg=''
msg = 'client1111'
client.sendto(msg.encode('utf-8'), ('127.0.0.1', 8080))
data, server_addr = client.recvfrom(1024)
print(data)
client.close()
2.3 Client 1
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 数据报协议-》udp
while True:
# msg=input('>>: ').strip() #msg=''
msg = 'client2222'
client.sendto(msg.encode('utf-8'), ('127.0.0.1', 8080))
data, server_addr = client.recvfrom(1024)
print(data)
client.close()
Three, socketserver source code analysis
ftpserver=socketserver.ThreadingTCPServer(('127.0.0.1', 8080),FtpServer)
ftpserver.serve_forever()
- Find the Order of Attributes: ThreadingTCPServer-> ThreadingMixIn-> TCPServer-> BaseServer
- Examples of ftpserver obtained, first find the class ThreadingTCPServer the __init__, found in the TCPServer thereby carry server_bind, server_active
- Get serve_forever under ftpserver, BaseServer found, the execution further self._handle_request_noblock (), which is likewise in the BaseServer
- Performing self._handle_request_noblock () performs addition request, client_address = self.get_request ((TCPServer is in self.socket.accept ())), then perform self.process_request (request, client_address)
- Found in ThreadingMixIn process_request, open multiple threads to deal with concurrency, and then execute process_request_thread, execution self.finish_request (request, client_address)
- The four partially completed cycle links, this part of the communication processing section began to enter, find finish_request in BaseServer, the trigger instantiate our own definition of class, go __init__ method, and our own definition of the class does not have this method, to its parent class is in BaseRequestHandler find ....
3.1 Source summary
- Tcp-based class of socketserver our own definition of
- self.server socket object that is
- self.request that is a link
- self.client_address namely client address
- Udp-based class of socketserver our own definition of
- self.request is a tuple (a first element is the data sent by the client, the server is a second portion of udp socket object), such as (b'adsf ', <socket.socket fd = 200, family = AddressFamily.AF_INET, type = SocketKind.SOCK_DGRAM, proto = 0, laddr = ( '127.0.0.1', 8080)>)
- self.client_address namely client address