python的socketserver模块实现TCP/UDP并发

基于tcp的套接字,关键就是两个循环,一个链接循环,一个通信循环

socketserver模块中分两大类:server类(解决链接问题)和request类(解决通信问题)

server类:

 request类:

 继承关系:

 

 

 以下述代码为例,分析socketserver源码:

ftpserver=socketserver.ThreadingTCPServer(('127.0.0.1',8080),FtpServer)
ftpserver.serve_forever()

查找属性的顺序:ThreadingTCPServer->ThreadingMixIn->TCPServer->BaseServer

  1. 实例化得到ftpserver,先找类ThreadingTCPServer的init,在TCPServer中找到,进而执行server_bind,server_active
  2. 找ftpserver下的serve_forever,在BaseServer中找到,进而执行self._handle_request_noblock(),该方法同样是在BaseServer中
  3. 执行self._handle_request_noblock()进而执行request, client_address = self.get_request()(就是TCPServer中的self.socket.accept()),然后执行self.process_request(request, client_address)
  4. 在ThreadingMixIn中找到process_request,开启多线程应对并发,进而执行process_request_thread,执行self.finish_request(request, client_address)
  5. 上述四部分完成了链接循环,本部分开始进入处理通讯部分,在BaseServer中找到finish_request,触发我们自己定义的类的实例化,去找init方法,而我们自己定义的类没有该方法,则去它的父类也就是BaseRequestHandler中找....

 

源码分析总结:

基于tcp的socketserver我们自己定义的类中的

  1. self.server即套接字对象
  2. self.request即一个链接
  3. self.client_address即客户端地址

基于udp的socketserver我们自己定义的类中的

  1. self.request是一个元组(第一个元素是客户端发来的数据,第二部分是服务端的udp套接字对象),如(b'adsf', )
  2. self.client_address即客户端地址

FtpServer

import socketserver
import struct
import json
import os
class FtpServer(socketserver.BaseRequestHandler):
    coding='utf-8'
    server_dir='file_upload'
    max_packet_size=1024
    BASE_DIR=os.path.dirname(os.path.abspath(__file__))
    def handle(self):
        print(self.request)
        while True:
            data=self.request.recv(4)
            data_len=struct.unpack('i',data)[0]
            head_json=self.request.recv(data_len).decode(self.coding)
            head_dic=json.loads(head_json)
            # print(head_dic)
            cmd=head_dic['cmd']
            if hasattr(self,cmd):
                func=getattr(self,cmd)
                func(head_dic)
    def put(self,args):
        file_path = os.path.normpath(os.path.join(
            self.BASE_DIR,
            self.server_dir,
            args['filename']
        ))

        filesize = args['filesize']
        recv_size = 0
        print('----->', file_path)
        with open(file_path, 'wb') as f:
            while recv_size < filesize:
                recv_data = self.request.recv(self.max_packet_size)
                f.write(recv_data)
                recv_size += len(recv_data)
                print('recvsize:%s filesize:%s' % (recv_size, filesize))

ftpserver=socketserver.ThreadingTCPServer(('127.0.0.1',8080),FtpServer)
ftpserver.serve_forever()

FtpClient

import socket
import struct
import json
import os

class MYTCPClient:
    address_family = socket.AF_INET

    socket_type = socket.SOCK_STREAM

    allow_reuse_address = False

    max_packet_size = 8192

    coding='utf-8'

    request_queue_size = 5

    def __init__(self, server_address, connect=True):
        self.server_address=server_address
        self.socket = socket.socket(self.address_family,
                                    self.socket_type)
        if connect:
            try:
                self.client_connect()
            except:
                self.client_close()
                raise

    def client_connect(self):
        self.socket.connect(self.server_address)

    def client_close(self):
        self.socket.close()

    def run(self):
        while True:
            inp=input(">>: ").strip()
            if not inp:continue
            l=inp.split()
            cmd=l[0]
            if hasattr(self,cmd):
                func=getattr(self,cmd)
                func(l)

    def put(self,args):
        cmd=args[0]
        filename=args[1]
        if not os.path.isfile(filename):
            print('file:%s is not exists' %filename)
            return
        else:
            filesize=os.path.getsize(filename)

        head_dic={'cmd':cmd,'filename':os.path.basename(filename),'filesize':filesize}
        print(head_dic)
        head_json=json.dumps(head_dic)
        head_json_bytes=bytes(head_json,encoding=self.coding)

        head_struct=struct.pack('i',len(head_json_bytes))
        self.socket.send(head_struct)
        self.socket.send(head_json_bytes)
        send_size=0
        with open(filename,'rb') as f:
            for line in f:
                self.socket.send(line)
                send_size+=len(line)
                print(send_size)
            else:
                print('upload successful')


client=MYTCPClient(('127.0.0.1',8080))

client.run()

一、socketserver模块基本使用

# 服务端
# encoding=utf-8
# auther:lsj
# 使用socketserver模块实现TCP协议

import socketserver
class MyRequestHandle(socketserver.BaseRequestHandler):
    def handle(self):
        print(self.request) # 如果是tcp协议,self.request-->conn
        print(self.client_address)  # self.client_address-->客户端的IP地址

s = socketserver.ThreadingTCPServer(('127.0.0.1',8080),MyRequestHandle)
s.serve_forever()  # 服务到永远,类似于下面的while True循环
# 服务端做两件事
# 第一件事:循环地从半连接池中取出链接请求与其建立双双向链接,拿到链接对象
# while True:
#     conn,client_addr = socketserver
#     启动一个线程(conn,client_addr)
# 第二件事:拿到链接对象,与其进行通讯循环--》写入handle方法中去

 socketserver模块的客户端(客户端、客户端1、客户端2代码都相同)

# 客户端
# encoding=utf-8
# auther:lsj
# 使用socketserver模块实现TCP协议的并发

from socket import *

client = socket(AF_INET,SOCK_STREAM) # AF_INET:基于网络通信,SOCK_STREAM:基于TCP协议
client.connect(('127.0.0.1',8080))

while True:
    cmd = input('请输入命令>>:').strip()
    if len(cmd) == 0:continue
    client.send(cmd.encode('utf-8'))
    cmd_res = client.recv(1024) # 本次接收,最大接收1024Bytes(我们发送的命令越简单越好所以1024就够了)
    print(cmd_res.decode('GBK')) # 强调:windows系统用gbk,Linux系统用utf-8。

 运行结果:

# 先启动服务端,后陆续启动客户端、客户端1、客户端2,查看服务端状态如下:
# 服务端启动后没有客户端启动
D:Python38python.exe D:/pycharm/oldboy_29/day037/day037_06socketserver模块基本使用/服务端.py
# 启动了客户端
<socket.socket fd=528, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8080), raddr=('127.0.0.1', 59384)>
('127.0.0.1', 59384)
# 启动了客户端1
<socket.socket fd=116, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8080), raddr=('127.0.0.1', 59386)>
('127.0.0.1', 59386)
# 启动了客户端2
<socket.socket fd=536, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8080), raddr=('127.0.0.1', 59390)>
('127.0.0.1', 59390)

 二、使用socketserver模块实现TCP协议的并发(修改服务端代码)

# encoding=utf-8
# auther:lsj
# 使用socketserver模块实现TCp协议的并发

import socketserver
class MyRequestHandle(socketserver.BaseRequestHandler):
    def handle(self):
        # print(self.request) # 如果是tcp协议,self.request-->conn
        # print(self.client_address)  # self.client_address-->客户端的IP地址
        while True:
            try:
                cmd = self.request.recv(1024)
                if len(cmd) == 0: break
                self.request.send(cmd.upper())
            except Exception:
                break
            self.request.close()
s = socketserver.ThreadingTCPServer(('127.0.0.1',8080),MyRequestHandle)
s.serve_forever()  # 服务到永远,类似于下面的while True循环
# 服务端做两件事
# 第一件事:循环地从半连接池中取出链接请求与其建立双双向链接,拿到链接对象
# while True:
#     conn,client_addr = socketserver
#     启动一个线程(conn,client_addr)
# 第二件事:拿到链接对象,与其进行通讯循环--》写入handle方法中去
先启动服务端,后陆续启动客户端、客户端1、客户端2,查看服务端状态如下:
# 启动客户端
D:Python38python.exe D:/pycharm/oldboy_29/day037/day037_06socketserver模块基本使用/客户端.py
请输入命令>>:hello
HELLO
请输入命令>>:

# 启动客户端1
D:Python38python.exe D:/pycharm/oldboy_29/day037/day037_06socketserver模块基本使用/客户端1.py
请输入命令>>:world
WORLD
请输入命令>>:

# 启动客户端2
D:Python38python.exe D:/pycharm/oldboy_29/day037/day037_06socketserver模块基本使用/客户端2.py
请输入命令>>:lsj
LSJ
请输入命令>>:

 三、使用socketserver模块实现UDP协议的并发

# 服务端
# encoding=utf-8
# auther:lsj
# 模拟服务端
# 服务端应该满足的特点:
"""
1、一直提供服务
2、并发地提供服务
"""
import socketserver

class MyRequestHanlde(socketserver.BaseRequestHandler):
    def handle(self):
        client_data = self.request[0]
        server=self.request[1]
        client_address=self.client_address
        print(self.request)
        print("客户端发来的数据%s"%client_data)
        server.sendto(client_data.upper(),client_address)
s =socketserver.ThreadingUDPServer(('127.0.0.1',8080),MyRequestHanlde)
s.serve_forever()
# 相当于whlie True:循环只负责循环的接收
# while True:
#     data,client_adr = server.recvfrom(1024)
    # 启动一个线程处理后续的事情(data,client_adr)
#     server.sendto(data.upper(),client_adr)
#     server.close()
# 客户端
# encoding=utf-8
# auther:lsj
# 模拟客户端
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
    msg=input('>>>:').strip()
    client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))
    res = client.recvfrom(1024)
    print(res)
client.close()

启动服务端和客户端、客户端1,运行结果如下:

# 客户端
D:Python38python.exe D:/pycharm/oldboy_29/day037/day037_06socketserver模块基本使用/基于UDP协议的socketserver的使用/客户端.py
>>>:hello
(b'HELLO', ('127.0.0.1', 8080))
>>>:world
(b'WORLD', ('127.0.0.1', 8080))
>>>:

# 客户端1
D:Python38python.exe D:/pycharm/oldboy_29/day037/day037_06socketserver模块基本使用/基于UDP协议的socketserver的使用/客户端1.py
>>>:lsj
(b'LSJ', ('127.0.0.1', 8080))
>>>:

# 服务端
D:Python38python.exe D:/pycharm/oldboy_29/day037/day037_06socketserver模块基本使用/基于UDP协议的socketserver的使用/服务端.py
(b'hello', <socket.socket fd=508, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('127.0.0.1', 8080)>)
客户端发来的数据b'hello'
(b'world', <socket.socket fd=508, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('127.0.0.1', 8080)>)
客户端发来的数据b'world'
(b'lsj', <socket.socket fd=508, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('127.0.0.1', 8080)>)
客户端发来的数据b'lsj'
原文地址:https://www.cnblogs.com/liunaixu/p/13111350.html