python-study

1.SERVER端

__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 = "\r%d%%" %(per_int)
    sys.stdout.write(temp)
    sys.stdout.flush()

ip_port = (\'127.0.0.1\',8011)
sk = socket.socket()
sk.bind(ip_port)
sk.listen(5)
# print (os.path.dirname(__file__))
print ("服务端启动...")
file_info = {}

while True:
    conn,addr = sk.accept()
    file_path = conn.recv(1024)
    file_path_str = str(file_path,\'utf8\')
    file_name = file_path_str.split(\'\\\')[-1]
    print (file_path_str)
    if os.path.exists(file_path_str):
        conn.sendall(bytes(\'2003\',\'utf8\'))      #告知客户端,服务器文件存在,可以下载
        conn.recv(1024)
        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)
        conn.sendall(bytes(file_info_json,"utf8"))

        client_info_recv = conn.recv(1024)
        client_info_recv_str = str(client_info_recv,\'utf8\')
        print (client_info_recv_str)
        client_repose_code = client_info_recv_str.split(\'|\')[0]
        client_file_size = client_info_recv_str.split(\'|\')[1]
        client_repose_code_int = int(client_repose_code)
        client_file_size_int = int(client_file_size)
        print (client_repose_code_int,client_file_size_int)

        if client_repose_code_int == 2004:      #客户端已经有了这个文件
            print ("客户端存在这个文件,现在开始断点续传...")
            file_obj = open(file_path_str,\'rb\')
            file_obj.seek(client_file_size_int)
            while client_file_size_int < file_size:
                    file_read = file_obj.read(1024)
                    conn.sendall(file_read)
                    client_file_size_int += len(file_read)
                    process_bar(client_file_size_int,file_size)
            file_obj.close()

        else :
            print ("客户端不存在这个文件,现在开始下载...")
            file_obj = open(file_path_str,\'rb\')
            while client_file_size_int < file_size:
                file_read = file_obj.read(1024)
                conn.sendall(file_read)
                client_file_size_int += len(file_read)
                process_bar(client_file_size_int,file_size)
            file_obj.close()

conn.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 = "\r%d%%" %(per_int)
    sys.stdout.write(temp)
    sys.stdout.flush()

FILE_PATH = "D:\\ftp\Rio.explorer.rmvb"         #你需要到服务器上去取得文件的目录
FILE_NAME = FILE_PATH.split(\'\\\')[-1]
print (FILE_NAME)

ip_port = (\'127.0.0.1\',8011)
sk = socket.socket()
sk.connect(ip_port)
file_info = {}
print ("客户端启动...")

while True:
    sk.sendall(bytes(FILE_PATH,"utf8"))
    file_exist_recv = sk.recv(1024)
    file_exist_recv_str = str(file_exist_recv,\'utf8\')
    if file_exist_recv_str == \'2003\':
        print ("服务器端文件存在,可以下载...")
        sk.sendall(bytes("ok",\'utf8\'))
        file_info_bytes = sk.recv(1024)
        file_info_str = str(file_info_bytes,"utf8")
        file_info = json.loads(file_info_str)
        server_file_name = file_info.get(\'file_name\')
        server_file_size = file_info.get(\'file_size\')
        print (file_info)

        if os.path.exists(server_file_name):
            client_file_size = os.stat(server_file_name).st_size
            print ("客户端已经有这个文件!")
            inp = input("文件是否需要续传?按y确认...")
            if inp == \'y\':
                sk.sendall(bytes(str(2004)+\'|\'+str(client_file_size),\'utf8\'))    #客户端已经有了这个文件

            client_file = open(server_file_name,\'ab\')
            while client_file_size < server_file_size:
                try:
                    data=sk.recv(1024)
                    if not data:
                        raise Exception
                except Exception:
                    break

                client_file.write(data)
                client_file_size += len(data)
                process_bar(client_file_size,server_file_size)
            client_file.close()

        else:                   #客户端没有这个文件
            client_file_size = 0
            # sk.sendall(bytes(str(2005|client_file_size),\'utf8\'))
            sk.sendall(bytes(str(2005)+\'|\'+str(client_file_size),\'utf8\'))
            client_file = open(server_file_name,\'wb\')
            while client_file_size < server_file_size:
                try:
                    data=sk.recv(1024)
                    if not data:
                        raise Exception
                except Exception:
                    break

                client_file.write(data)
                client_file_size += len(data)
                process_bar(client_file_size,server_file_size)
            client_file.close()

    else:
        print ("服务器端文件不存在,请查看文件输入是否正确!")
sk.close()

 

分类:

技术点:

相关文章:

  • 2021-11-25
  • 2022-02-09
  • 2021-12-11
  • 2021-11-28
  • 2022-01-23
  • 2022-12-23
  • 2021-11-30
  • 2021-07-16
猜你喜欢
  • 2021-09-13
  • 2021-10-03
  • 2021-06-10
  • 2021-12-15
  • 2021-09-25
  • 2021-08-03
  • 2021-10-24
相关资源
相似解决方案