文件的上传下载

# 文件的上传下载客户端普通版(会有粘包的情况出现)
import os
import json
import socket

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

menu = {'1':'upload','2':'download'}
for k,v in menu.items():
    print(k,v)
num = input('请输入功能选项>>>')

if num == '1': # 上传
    dic = {'opt':menu.get(num),'filename':None,'filesize':None} # filesize 按字节去内容防止超出内容大小报错

    abs_path = input('请输入一个绝对路径:')
    filename = os.path.basename(abs_path) # 获取要上传的文件名
    filesize = os.path.getsize(abs_path) # 获取文件的大小
    dic['filename'] = filename
    dic['filesize'] = filesize
    str_dic = json.dumps(dic) # 把字典序列化
    sk.send(str_dic.encode('utf-8'))

    with open(abs_path,'rb') as f:
        while filesize:
            content = f.read(1024)
            sk.send(content)
            filesize -= len(content)

elif num == '2': # 下载
    dic = {'opt':menu.get(num),'filename':None,'filesize':None}
    abs_path = input('请输入要下载文件的绝对路径:')
    filename = os.path.basename(abs_path)
    dic['abs_path'] = abs_path
    dic['filename'] = filename
    str_dic = json.dumps(dic)
    sk.send(str_dic.encode('utf-8'))

    res_dic = sk.recv(1024).decode('utf-8')
    result = json.loads(res_dic)

    with open(filename,'ab') as f:
        while dic['filesize']: # dic['filename'] = 0时停止循环
            content = sk.recv(1024) # 这里接收到的是字节
            f.write(content)
            dic['filesize '] -= len(content)
# 文件的上传下载服务器端普通版(会有粘包的情况出现)
import os
import socket
import json

sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()
conn,addr = sk.accept()

str_dic = conn.recv(1024).decode('utf-8')
dic = json.loads(str_dic) # 反序列化

if dic['opt'] == 'upload': # 上传
    filename = '' + dic['filename']
    with open(filename,'ab') as f:
        while dic['filesize']: # dic['filename'] = 0时停止循环
            content = conn.recv(1024)
            f.write(content)
            dic['filesize'] -= len(content)

if dic['opt'] == 'download': # 下载
    abs_path = dic['abs_path']
    filesize = os.path.getsize(abs_path)
    dic['filesize'] = filesize
    str_dic = json.dumps(dic)
    conn.send(str_dic.encode('utf-8'))

    with open(abs_path,'rb') as f:
        while filesize:
            content = f.read(1024)
            conn.send(content)
            filesize -= len(content)
# 文件的上传下载客户端优化版(不会有粘包的情况出现)
import os
import json
import socket
import struct

sk = socket.socket()
sk.connect(('127.0.0.1',8080))
menu = {'1':'upload','2':'download'}
for k,v in menu.items():
    print(k,v)
num = input('请输入功能选项>>>')

if num == '1': # 上传
    dic = {'opt':menu.get(num),'filename':None,'filesize':None} # filesize 按字节去内容防止超出内容大小报错

    abs_path = input('请输入一个绝对路径:')
    filename = os.path.basename(abs_path) # 获取要上传的文件名
    filesize = os.path.getsize(abs_path) # 获取文件的大小
    dic['filename'] = filename
    dic['filesize'] = filesize
    str_dic = json.dumps(dic) # 把字典序列化
    len_dic = len(str_dic)
    b_len_dic = struct.pack('i',len_dic) # 用struck将报头长度这个数字转成固定长度:4个字节
    sk.send(b_len_dic + str_dic.encode('utf-8'))

    with open(abs_path,'rb') as f:
        while filesize:
            content = f.read(1024)
            sk.send(content)
            filesize -= len(content)

elif num == '2': # 下载
    dic = {'opt':menu.get(num),'filename':None,'filesize':None}
    abs_path = input('请输入要下载文件的绝对路径:')
    filename = os.path.basename(abs_path)
    dic['abs_path'] = abs_path
    dic['filename'] = filename
    str_dic = json.dumps(dic)
    sk.send(str_dic.encode('utf-8'))

    res_dic = sk.recv(1024).decode('utf-8')
    result = json.loads(res_dic)

    with open(filename,'ab') as f:
        while dic['filesize']: # dic['filename'] = 0时停止循环
            content = sk.recv(1024)
            f.write(content)
            dic['filesize '] -= len(content)
# 文件的上传下载服务器端优化版(不会有粘包的情况出现)
import os
import socket
import json
import struct

sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()
conn,addr = sk.accept()

b_len_dic = conn.recv(4) # 接收4个字节正好是字典的长度
len_dic = struct.unpack('i',b_len_dic)[0] # 解决粘包的办法
str_dic = conn.recv(len_dic).decode('utf-8')
dic = json.loads(str_dic) # 反序列化

if dic['opt'] == 'upload': # 上传
    filename = '' + dic['filename']
    with open(filename,'ab') as f:
        while dic['filesize']: # dic['filename'] = 0时停止循环
            content = conn.recv(1024)
            f.write(content) # 以字节的形式写进去
            dic['filesize'] -= len(content)

if dic['opt'] == 'download': # 下载
    abs_path = dic['abs_path']
    filesize = os.path.getsize(abs_path)
    dic['filesize'] = filesize #
    str_dic = json.dumps(dic)
    conn.send(str_dic.encode('utf-8'))

    with open(abs_path,'rb') as f:
        while filesize:
            content = f.read(1024)
            conn.send(content)
            filesize -= len(content)

 

posted on 2018-08-14 22:03  窮山霧繞(静妙)  阅读(119)  评论(0编辑  收藏  举报

导航