二十七、发送文件,进度条(下载),UDP,socketserver模块

一、发送文件

客户端:
import socket
import json
import os
import struct

client = socket.socket()
client.connect(('127.0.0.1', 8080))

while True:
    # 获取电影列表 循环展示
    MOVIE_DIR = r'D:\资料\选课系统\视频'
    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.再发文件数据(打开文件循环发送)
            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')
服务端
import socket
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:
            header_len = conn.recv(4)
            # 解析字典报头
            header_len = struct.unpack("i", header_len)[0]
            # 再接收字典数据
            header_dic = conn.recv(header_len)
            real_dic = json.loads(header_dic.decode("utf8"))
            # 获取数据长度
            total_size = real_dic.get("file_size")
            # 循环接收并写入文件
            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)
                    val = int(recv_size / total_size * 100)
                    print("%s%%\r" % val, end="")
                print("上传ok")
        except ConnectionAbortedError as e:
            print(e)
    conn.close()

二、进度条

import time
for i in range(1, 100):
    msg = "%s%%\r" % i
    print(msg, end="")
    time.sleep(0.05)
 # \r转义是覆盖重写
import os

# 1.读取文件大小
file_size = os.stat("D:\code\day21网络编程2\嘿嘿.mp4").st_size
read_size = 0
with open("D:\code\day21网络编程2\嘿嘿.mp4", "rb") as f:
    while True:
        chunk = f.read(1024)
        read_size += len(chunk)
        val = int(read_size / file_size * 100)
        print("%s%%\r" % val, end="")

三、UDP

数据报协议(自带报头)
没有双向通道 通信类似于发短信
1.udp协议客户端允许发空
2.udp协议不会粘包
3.udp协议服务端不存在的情况下,客户端照样不会报错?
4.udp协议支持并发

客户端
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)

服务端
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)

四、socketserver模块

 TCP:

服务端
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()  # 启动该服务对象

客户端
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'))
TCP

UDP:

服务端
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()  # 启动该服务对象

客户端
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)
UDP

 

posted @ 2019-08-08 17:04  凯帅  阅读(318)  评论(0编辑  收藏  举报