zoukankan      html  css  js  c++  java
  • FTP服务器之上传

    1.SERVER端

    __author__ = 'alex'
    #coding:utf-8
    import socket
    import os
    import sys
    import json
    
    
    ip_port = ('127.0.0.1',8009)
    sk = socket.socket()
    sk.bind(ip_port)
    sk.listen(5)
    # print (os.path.dirname(__file__))
    print ("服务端启动...")
    
    
    while True:
        conn,addr = sk.accept()
        file_info_bytes = conn.recv(1024)
        file_info_str = str(file_info_bytes,"gbk")
        file_info = json.loads(file_info_str)
        client_file_name = file_info.get('file_name')
        client_file_size = file_info.get('file_size')
        print (file_info)
    
        if os.path.exists(client_file_name):
            print ("文件存在!")
            server_file_size = os.stat(client_file_name).st_size
            print (server_file_size)
            conn.sendall(bytes(str(server_file_size),"gbk"))
            server_file = open(client_file_name,'ab')
            while server_file_size < client_file_size:
                try:
                    data=conn.recv(1024)
                    if not data:
                        raise Exception
                except Exception:
                    break
    
                server_file.write(data)
                server_file_size += len(data)
            server_file.close()
        else:
            print ("文件不存在!")
            server_file_size = 0
            print (server_file_size)
            conn.sendall(bytes(str(server_file_size),"gbk"))
            # has_received = 0
            server_file = open(client_file_name,'wb')
            while server_file_size < client_file_size:
                try:
                    data=conn.recv(1024)
                    if not data:
                        raise Exception
                except Exception:
                    break
    
                server_file.write(data)
                server_file_size += len(data)
            server_file.close()

    2.CLIENT端

    __author__ = 'alex'
    #coding:utf-8
    import socket
    import os
    import sys
    import json
    
    def process_bar(num,total):
        percent = float(num)/float(total)
        per_int = int(percent*100)
        # print (percent,per_int)
        # print (">"*per_int + "%d%%" %(per_int+1))
        temp = "
    %d%%" %(per_int)
        sys.stdout.write(temp)
        sys.stdout.flush()
    
    FILE_PATH = "D:\ftp\Rio.explorer.rmvb"
    FILE_NAME = FILE_PATH.split('\')[-1]
    
    ip_port = ('127.0.0.1',8009)
    sk = socket.socket()
    sk.connect(ip_port)
    file_info = {}
    print ("客户端启动...")
    
    while True:
    
        if os.path.exists(FILE_PATH):
            file_size = os.stat(FILE_PATH).st_size
            file_info['file_name'] = FILE_NAME
            file_info['file_size'] = file_size
            file_info_json = json.dumps(file_info)
            sk.sendall(bytes(file_info_json,"gbk"))
            server_file_size = sk.recv(1024)
            server_file_size_str = str(server_file_size,"gbk")
            server_file_size_int = int(server_file_size_str)
            print (server_file_size_str)
            if int(server_file_size_str) != 0:
                inp = input("服务器上存在此文件,是否续传?")
                if inp == 'y':
                    file_obj = open(FILE_PATH,'rb')
                    file_obj.seek(server_file_size_int)
                    # print (file_seek)
                    print ("开始续传...")
                    while server_file_size_int < file_size:
                        file_read = file_obj.read(1024)
                        sk.sendall(file_read)
                        server_file_size_int += len(file_read)
                        process_bar(server_file_size_int,file_size)
                    file_obj.close()
                else:
                    break
            else:
                print ("服务器端没有这个文件,要从开头开始发送...")
                file_obj = open(FILE_PATH,'rb')
                while server_file_size_int < file_size:
                    file_read = file_obj.read(1024)
                    sk.sendall(file_read)
                    server_file_size_int += len(file_read)
                    process_bar(server_file_size_int,file_size)
                file_obj.close()
    
        else:
            print ("本地文件不存在!")
    
    
    sk.close()
  • 相关阅读:
    linux nat style
    vmware tools install
    linux network
    sql group by max
    实例 | tp5使用七牛云上传图片和文件/删除文件
    菜鸟如何使用composer
    浅谈数据库用户表结构设计,第三方登录
    app登陆,注册,第三方登陆数据设计及业务流程
    数据库设计——评论回复功能
    app接口设计之token的php实现
  • 原文地址:https://www.cnblogs.com/python-study/p/5815606.html
Copyright © 2011-2022 走看看