![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1.异常处理 什么是异常? 程序在运行过程中出现了不可预知的错误 并且该错误没有对应的处理机制,那么就会以异常的形式表现出来 造成的影响就是整个程序无法再正常运行 异常的结构 1.异常的类型:NAMEERROR 2.异常的信息:name 'fdsdfsdf' is not defined 3.异常的位置:Traceback (most recent call last): File "D:/python脱产10期视频/day29/01 异常处理.py", line 1, in <module> fdsdfsdf 异常的种类 分为两大类 1.语法错误 是你程序立刻就能解决的,这种错误是不能被容忍的 语法上的错误 发现之后应该立刻解决 2.逻辑错误 这种错是可以被容忍的 因为一眼看不出来 针对逻辑上的错误 可以采用异常处理机制进行捕获 常见的错误类型 NAMERROR 名字错误 SyntaxError 语法错误 KeyError 键不存在 ValueError 值错误 IndexError 索引错误 如何避免 异常处理 在你认为可能会出现bug的代码块上方try一下:注意try内部的代码块越少越好 try: 可能出错的代码 except 出错的类型 as e: # 将报错信息赋值给变量e 出错之后的处理机制 2.UDP通信 数据报协议(自带报头) 没有双向通道 通信类似于发短信 1.udp协议客户端允许发空 2.udp协议不会粘包 3.udp协议服务端不存在的情况下,客户端照样不会报错? 4.udp协议支持并发 写一个简易版本的QQ UDP类似于发短信 TCP类似于打电话 并发:看起来像同时运行的 并行:真正意义上的同时运行 SocketServer 网络编程 TCP解决粘包问题 上传文件下载文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
res = {"name":""} # print(res['password'])# KeyError: 'password' print(res.get('password','dsfdfsfdd')) # dsfdfsfdd print(res.get('password')) # None # int('sdsfdsfdfdfdf') # ValueError: invalid literal for int() with base 10: 'sdsfdsfdfdfdf' # l = [1,2,3,4] # l[111] # IndexError: list index out of range try: # name # NameError l = [1,2,3] # l[111] # indexerror d = {'name': 'jason'} # d['password'] # keyerror except NameError: print('NameError') except IndexError: print('indexerror') except KeyError: print('keyerror') """ 错误发生之后 会立刻停止代码的运行 执行except语句 比对错误类型 """ try: # name # NameError l = [1,2,3] # l[111] # indexerror d = {'name': 'jason'} # d['password'] # keyerror except BaseException: # 万能异常 所有的异常类型都被捕获 print('老子天下无敌') else: print('被检测的代码没有任何的异常发生 才会走else') finally: print('无论被检测的代码有没有异常发生 都会在代码运行完毕之后执行我') """主动抛出异常""" if 'egon' == 'DSB': pass else: raise TypeError('净说大实话') # 关键字raise 就是主动抛出异常 """assert 断言,预言""" l = [1,2,3] assert len(l) > 0 # 断言 预言 # 猜某个数据的状态 猜对了 不影响代码执行 正常走 # 猜错了 直接报错 """自定义异常""" class MyError(BaseException): def __init__(self,msg): super().__init__() self.msg = msg def __str__(self): return f'<dfsdf{self.msg}ssdffff>' raise MyError('我自己定义的异常') # # 主动抛出异常其实就是将异常类的对象打印出来,会走__str__方法
socketserver模块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket client = socket.socket() client.connect(('127.0.0.1',8080)) while True: client.send(b'hello') data = client.recv(1024) print(data.decode('utf-8'))
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): # print("来啦,老弟") while True: data = self.request.recv(1024) print(self.client_address) # 客户端地址 print(data.decode('utf-8')) self.request.send(data.upper()) if __name__ == '__main__': """只要有客户端连接 会自动交给自定义类中的handle方法去处理""" server = socketserver.ThreadingTCPServer(('127.0.0.1',8080),MyServer) # 创建一个基于TCP的对象 server.serve_forever() # 启动该服务对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket import time client = socket.socket(type=socket.SOCK_DGRAM) server_address = ('127.0.0.1',8080) while True: client.sendto(b'hello',server_address) data,addr = client.recvfrom(1024) print(data.decode('utf-8'),addr) time.sleep(1)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): # print('来啦 老弟') while True: data,sock = self.request print(self.client_address) # 客户端地址 print(data.decode('utf-8')) sock.sendto(data.upper(),self.client_address) if __name__ == '__main__': """只要有客户端连接 会自动交给自定义类中的handle方法去处理""" server = socketserver.ThreadingUDPServer(('127.0.0.1',8080),MyServer) # 创建一个基于TCP的对象 server.serve_forever() # 启动该服务对象
TCP与UDP区别
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket client = socket.socket(type=socket.SOCK_DGRAM) server_address = ('127.0.0.1',8080) # while True: # msg = input(">>>:") client.sendto(b'hello',server_address) client.sendto(b'hello',server_address) client.sendto(b'hello',server_address) # data, server_addr = client.recvfrom(1024) # print(data)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket server = socket.socket(type=socket.SOCK_DGRAM) # sock_dgram server.bind(('127.0.0.1',8080)) # while True: data,addr = server.recvfrom(1024) print(data) data,addr1 = server.recvfrom(1024) print(data) data,addr2 = server.recvfrom(1024) print(data)
UDP基本使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket client = socket.socket(type=socket.SOCK_DGRAM) # 不需要建立连接 直接进入通信循环 server_address = ('127.0.0.1',8080) while True: client.sendto(b'hello',server_address) data,addr = client.recvfrom(1024) print('服务端发来的数据', data) print('服务端的地址', addr)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket server = socket.socket(type=socket.SOCK_DGRAM) # UDP协议 server.bind(('127.0.0.1',8080)) # UDP 不需要设置半连接池,他也没有半连接池概念 # 因为没有双向通道 不需要accept 直接就是通信循环 while True: data,addr = server.recvfrom(1024) print('数据',data) # 客户端发来的消息 print('地址:', addr) # 客户端的地址 server.sendto(data.upper(),addr) """ 数据 b'hello' 地址: ('127.0.0.1', 52918) 数据 b'hello' 地址: ('127.0.0.1', 52918) 数据 b'hello' 地址: ('127.0.0.1', 52918) """
UDP实现建议版本的qq
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket client = socket.socket(type=socket.SOCK_DGRAM) # UDP server_address = ('127.0.0.1',8080) while True: msg = input("clint:>>").strip() msg = '来自客户端1的消息:%s' % msg client.sendto(msg.encode('utf-8'),server_address) data,server_addr = client.recvfrom(1024) print(data.decode('utf-8'))
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket client = socket.socket(type=socket.SOCK_DGRAM) # UDP server_address = ('127.0.0.1', 8080) while True: msg = input("clint:>>").strip() msg = '来自客户端2的消息:%s' % msg client.sendto(msg.encode('utf-8'), server_address) data, server_addr = client.recvfrom(1024) print(data.decode('utf-8'))
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket server = socket.socket(type=socket.SOCK_DGRAM) server.bind(('127.0.0.1',8080)) while True: data,addr = server.recvfrom(1024) print(data.decode('utf-8')) msg = input("服务器指令:>>>").strip() server.sendto(msg.encode('utf-8'),addr)
发送大文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket import json import os import struct client = socket.socket() client.connect(('127.0.0.1',8080)) while True: # 获取电影列表 循环展示 MOVIE_DIR = r'D:\Python10期\day29大文件上传 UDP协议 异常处理 自定义socketserver\视频' movie_list = os.listdir(MOVIE_DIR) # print(movie_list) for i,movie in enumerate(movie_list,1): print(i,movie) # 用户选择 choice = input('please choice movie to upload>>>:') # 判断是否是数字 if choice.isdigit(): # 将字符串数字转为int choice = int(choice) - 1 # 判断用户选择在不在列表范围内 if choice in range(0, len(movie_list)): # 获取到用户想上传的文件路径 path = movie_list[choice] # 拼接文件的绝对路径 file_path = os.path.join(MOVIE_DIR,path) # 获取文件大小 file_size = os.path.getsize(file_path) # 定义一个字典 res_d = { 'file_name': '性感荷官在线发牌.mp4', 'file_size': file_size, 'msg': '注意身体,多喝营养快线' } # 序列化字典 json_d = json.dumps(res_d) json_bytes = json_d.encode('utf-8') # 1.先制作字典格式的报头 header = struct.pack('i', len(json_bytes)) # 2.发送字典的报头 client.send(header) # 3.再发字典 client.send(json_bytes) # 4.再发文件数据(打开文件循环发送) print(11111) with open(file_path, 'rb') as f: for line in f: client.send(line) else: print('not in range') else: print('must be a number')
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket import os import json import struct server = socket.socket() server.bind(('127.0.0.1',8080)) server.listen(5) while True: conn,addr = server.accept() while True: try: # 1.接受报头 header_len = conn.recv(4) # 2.解析报头 header_len = struct.unpack('i',header_len)[0] # 3.接收字典数据 header_dic = conn.recv(header_len) real_dic = json.loads(header_dic.decode('utf-8')) # 4.获取数据长度 total_size = real_dic.get('file_size') # 5.循环接收 ,并写入文件 recv_size = 0 with open(real_dic.get('file_name'),'wb') as f: while recv_size < total_size: data = conn.recv(1024) f.write(data) recv_size += len(data) print('上传成功') except ConnectionResetError as e: print(e) break conn.close()
性感荷官在线发牌.mp4