博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
断点续传
阅读量:5113 次
发布时间:2019-06-13

本文共 4887 字,大约阅读时间需要 16 分钟。

#FTP\conf\settings.py
import osimport sysBASE_DIR=os.path.dirname(os.path.dirname(__file__))BASE_CLIENT=os.path.join(BASE_DIR,"db","client")BASE_SERVER=os.path.join(BASE_DIR,"db","server")BASE_USE=os.path.join(BASE_DIR,'db','user')BASE_admin=os.path.join(BASE_DIR,'db','admin')sys.path.append(BASE_DIR)

 

FTP\bin\head.py

import jsonimport osimport structfrom conf.settings import *def recv_head(client):    head_len=client.recv(4)    if not head_len:        raise Exception("对方已下线")    lens= struct.unpack("i",head_len)[0]    # print(lens)    rep= json.loads(client.recv(lens))    return repdef send_rep(client,rep):    jdata=json.dumps(rep).encode("utf-8")    # print(jdata)    client.send(struct.pack('i',len(jdata)))    client.send(jdata)# 判断服务端文件def choice(path):    name=os.path.split(path)[1]    # print(name)    if name in os.listdir(BASE_SERVER):        return True    return False# 判断客户端文件def clientfile(path):    name = os.path.split(path)[1]    # print(name)    if name in os.listdir(BASE_CLIENT):        return True    return False

 

 

FTP\bin\FTPserver.py

mport osimport socketimport structimport jsonfrom bin.head import *from conf.settings import *server = socket.socket()server.bind(("127.0.0.1",8081))server.listen(5)def upload(client):    # 接收json数据    rep = recv_head(client)    file_path = os.path.join(BASE_SERVER, rep['filename'])    datasunm = 0    while int(rep["filesize"]) >= datasunm:        if choice(rep['filename']):            file_size = os.path.getsize(file_path)            client.send(str(file_size).encode("utf-8"))            with open(file_path, "ab") as rb:                client_data = client.recv(2048)                rb.write(client_data)                datasunm += len(client_data)                print(len(client_data))        else:            client.send("F".encode("utf-8"))            with open(file_path, "wb") as rb:                client_data = client.recv(2048)                rb.write(client_data)                datasunm += len(client_data)                print(len(client_data))    client.close()# 下载def download(client):    #  获取文件信息    reps = recv_head(client)    print(reps)    # 服务端文件地址    file_path = os.path.join(BASE_SERVER, reps['filename'])    # 本地文件大小    file_size = os.path.getsize(file_path)    # 发送系统文件大小    dic = {"filesize": file_size}    send_rep(client, dic)    requ = recv_head(client)    print(requ['filesize'])    index = int(requ['filesize'])    with open(file_path, "rb") as rf:        rf.seek(index)        while index <= file_size:            data = rf.read(2048)            client.send(data)            rf.flush()            index += len(data)            print(index)            if index == file_size: break        rf.close()        print("发送完成")

 

def recv_heads(server):    while True:        try:           client,addr=server.accept()           # 上传            upload(client)            download(client)        except ConnectionResetError as e:               print("客户端断开")

  

 

FTP\bin\FTPclient.py

from conf.settings import *import socketfrom bin.head import *client=socket.socket()client.connect(('127.0.0.1',8081))def upload(client,size,path):    file_size = client.recv(1028).decode("utf-8")    # print(file_size)    if file_size == "F":        index = 0        print(index)    else:        index = int(file_size)        print(index)    with open(path, "rb") as rf:        rf.seek(index)        while index <= size:            data = rf.read(2048)            client.send(data)            rf.flush()            index += len(data)            print(index)            if index == size: break        rf.close()        print("下载完成")# 上传端def sender(client,path):    if  not os.path.exists(path):        print("文件不存在")        return    name=os.path.split(path)[1]    size=os.path.getsize(path)    # print(size)    rep={"filename":name,"filesize": size}    send_rep(client,rep)    # 上传端    upload(client, size,path)# 下载端def download(client,name):    dic = {"filename": name}    send_rep(client, dic)    file_size=recv_head(client)    # 系统文件大小    size=int(file_size['filesize'])    # 存储文件地址    filepath = os.path.join(BASE_CLIENT, name)    # # 如果有文件不覆写    datasunm = 0    while datasunm < size:        # 如果目录内存在文件        if clientfile(filepath):           filesize=os.path.getsize(filepath)           dic={"filesize":filesize}           send_rep(client,dic)           with open(filepath,"ab") as rf:               data=client.recv(2048)               rf.write(data)               datasunm+=len(data)               print(datasunm)        else:            # 如果目录内文件不存在            filesize=0            dic = {"filesize": filesize}            send_rep(client, dic)            with open(filepath, "wb") as rf:                data = client.recv(2048)                rf.write(data)                datasunm += len(data)                print(datasunm)    client.close()

  

 

转载于:https://www.cnblogs.com/xzcvblogs/p/11005593.html

你可能感兴趣的文章
查询数据(后台到前台传递数据,显示数据)
查看>>
集群tomcat+apache配置文档
查看>>
VMware Tools安装
查看>>
2019.04.09 电商20 购物车的展示
查看>>
Linux上架设boost的安装及配置过程
查看>>
[转载]加密算法库Crypto——nodejs中间件系列
查看>>
zoj 2286 Sum of Divisors
查看>>
OO5~7次作业总结
查看>>
如何判断主机是大端还是小端(字节序)
查看>>
Centos7 日志查看工具
查看>>
使用Xshell密钥认证机制远程登录Linux
查看>>
BZOJ2459 : [BeiJing2011]神秘好人
查看>>
Django入门示例之被解放的姜戈——01 初试天涯(安装及启动)
查看>>
OpenCV之响应鼠标(三):响应鼠标信息
查看>>
python7 数据类型的相互转化 字符编码
查看>>
Android 画图之 Matrix(一)
查看>>
React Native - 2 控件Flexbox
查看>>
前缀和
查看>>
Jquery插件汇集:
查看>>
Linux 启动、关闭、重启网络服务的两种方式
查看>>