(p ython +tcp+tcp)文件传输

#情景:向服务器上传文件,发送文件,获取目录,和退出

 

服务器端:

"""
/etc/python3
@File     : miker_server.py
@Time     : 2020/7/15 下午3:29
@Author   : wangyongqi
@Email    : 92644827@qq.com
@SOftware : PyCharm 
"""


import os
import time
from socket import *
from threading import Thread

HOST = "0.0.0.0"
PORT = 8888
ADDR = (HOST, PORT)

FTP = "/home/tarena/img/"


class FTP_Server(Thread):
    def __init__(self, connfd):
        super().__init__()
        self.connfd = connfd
    #目录处理
    def list_on(self):
        file_list = os.listdir(FTP)
        if not file_list:
            self.connfd.send(b'FAIL')  # 列表为空
            return
        else:
            self.connfd.send(b'OK')
            time.sleep(0.1)
            data = "\n".join(file_list)
            self.connfd.send(data.encode())
    #下载
    def list_get(self):
        data = self.connfd.recv(1024).decode()
        print(data)
        file_list = os.listdir(FTP)
        print(file_list)
        if data in file_list:
            self.connfd.send(b'OK')
            data = "/home/tarena/img/" + data
            with open(data, 'rb') as list_get_wb:
                stay = list_get_wb.read()
                print(stay)
                self.connfd.send(stay)
        else:
            self.connfd.send(b'FAIL')
    #上传
    def list_up(self):
        data = self.connfd.recv(1024).decode()
        print(data)
        file_list = os.listdir(FTP)

        if data in file_list:
            self.connfd.send(b'FAIL')
        else:
            self.connfd.send(b'OK')
            with open("/home/tarena/img/" + data, 'wb') as list_get_wb:
                while True:
                    file = self.connfd.recv(1024)
                    print(file)
                    if not file:
                        break
                    list_get_wb.write(file)



    def run(self):
        # 总分模式
        while True:
            # 某个客户端所有的请求
            data = self.connfd.recv(1024).decode()
            print(data)
            # 更具不同的请求做不同处理
            if data == 'LIST':
                self.list_on()
            elif data == 'get':
                self.list_get()
            elif data == 'up':
                self.list_up()
            elif data=='exit':
                break
        self.connfd.close()


def main():
    sock = socket()
    # 绑定
    sock.bind(ADDR)
    # 监听队列
    sock.listen(5)
    print("Listen the prot %s" % PORT)


    while True:
        try:
            connfd, addr = sock.accept()
            print("Connect from", addr)
        except KeyboardInterrupt:
            sock.close()
            return
        t = FTP_Server(connfd)
        t.setDaemon(True)
        t.start()


if __name__ == '__main__':
    main()

 

客户端:

"""
/etc/python3
@File     : miker_connect.py
@Time     : 2020/7/15 下午3:29
@Author   : wangyongqi
@Email    : 92644827@qq.com
@SOftware : PyCharm 
"""

from socket import *
from time import *

# 服务器地址
ADDR = ('127.0.0.1', 8888)
mains = "  ========== \n||1.查看目录内的内容  ||\n||2.下载文件         ||\n||3.上传文件         ||\n||4.退出          ||\n  ==========  "


class FTPClient:
    def __init__(self, sock):
        self.sock = sock

    def list(self):
        self.sock.send(b"LIST")  # 发送请求
        result = self.sock.recv(128).decode()  # 回复 字符串
        # 根据回复分情况讨论
        if result == 'OK':
            file = self.sock.recv(1024 * 1024).decode()
            print(file)
        else:
            print("文件库空")

    def list_get(self):
        self.sock.send(b"get")
        sleep(1)
        mains_input = input("请输入文件名")
        print(mains_input)
        self.sock.send(mains_input.encode())  # 发送请求
        result = self.sock.recv(1024)  # 回复 字符串
        result = result.decode()
        print(result)
        if result == 'OK':
            with open(mains_input, 'wb') as list_get_wb:
                while True:
                    file = self.sock.recv(1024 * 1024)
                    print(file)
                    if not file:
                        break
                    else:
                        list_get_wb.write(file)
                    break
        else:
            print("无这文件")

    def list_up(self):
        self.sock.send("up".encode())
        mains_input = input("请输入文件名")
        self.sock.send(mains_input.encode())  # 发送请求
        result = self.sock.recv(1024)  # 回复 字符串
        result = result.decode()
        print(result)
        if result == 'OK':
            with open(mains_input, 'rb') as list_up_rb:
                file=list_up_rb.read()
                print(file)
                self.sock.send(file)
        else:
            print("已有这个文件")

    def exit(self):
        self.sock.send("exit".encode())



def main():
    sock = socket()

    sock.connect(ADDR)

    ftp = FTPClient(sock)
    while True:
        mains_input = input(f"{mains}\n>>")
        if mains_input == "1":
            ftp.list()
        elif mains_input == "2":
            ftp.list_get()
        elif mains_input == "3":
            ftp.list_up()
        elif mains_input=='4':
            ftp.exit()
            break


if __name__ == '__main__':
    main()

 

上一篇:php-fpm7.0 慢查询设置及说明


下一篇:Mycat读写分离 & 分库分表