码迷,mamicode.com
首页 > Web开发 > 详细

FTP服务器之上传

时间:2016-08-28 19:25:52      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

 

 

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 = "\r%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()

 

FTP服务器之上传

标签:

原文地址:http://www.cnblogs.com/python-study/p/5815606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!