0


文件服务器

#ftpserver.pyfrom socket import*from threading import Thread

import os,time

#全局变量
HOST ='0.0.0.0'
PORT=8080
ADDR =(HOST,PORT)
FTP  ="FTP/"#文件库位置#创建类实现服务器文件处理功能classFTPServer(Thread):"""
    查看列表,下载,上传,退出
    """def__init__(self,connfd):
        self.connfd = connfd
        super().__init__()defdo_list(self):
        files = os.listdir(FTP)ifnot files:
            self.connfd.send('文件库为空'.encode())returnelse:

            self.connfd.send(b'OK')
            time.sleep(0.1)#拼接文件
        filelist=''forfilein files:iffile[0]!='.'and \
                os.path.isfile(FTP+file):
                filelist +=file+'\n'
        self.connfd.send(filelist.encode())defdo_get(self,filename):try:
            f =open(FTP+filename,'rb')except Exception:
            self.connfd.send('文件不存在'.encode())returnelse:
            self.connfd.send(b'OK')
            time.sleep(0.1)whileTrue:
                data = f.read(1024)ifnot data:
                    time.sleep(0.1)
                    self.connfd.send(b'##')return
                self.connfd.send(data)defdo_put(self, filename):if os.path.exists(FTP+filename):
              self.connfd.send('文件已经存在'.encode())returnelse:

              self.connfd.send(b'OK')

              f =open(FTP+filename,'wb')whileTrue:
                   data = self.connfd.recv(1024)if data ==b'##':break
                   f.write(data)

              f.close()#打开失败文件不存在#循环接受请求,分情况调用功能函数defrun(self):whileTrue:
            data = self.connfd.recv(1024).decode()ifnot data or data =='Q':return#线程结束elif data =='L':
                self.do_list()elif data[0]=='G':
                filename = data.split(' ')[-1]
                self.do_get(filename)elif data[0]=='P':
                filename = data.split(' ')[-1]
                self.do_put(filename)#搭建网络服务端模型defmain():
    s = socket()
    s.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)
    s.bind(ADDR)
    s.listen(5)# 处理僵尸进程whileTrue:try:
            c, addr = s.accept()print('Connect from ', addr)except KeyboardInterrupt:
            os._exit(0)except Exception as e:print(e)continue# 创建子进程处理客户端请求
        client = FTPServer(c)

        client.start()if __name__  =="__main__":
    main()

ftp_client

"""
ftp 文件服务,客户端
"""import sys
from socket import*#服务器地址
ADDR =('192.168.87.128',8080)#客户端文件处理类classFTPClient:def__init__(self,sockfd):
        self.sockfd = sockfd
    defdo_list(self):
        self.sockfd.send(b'L')#发送请求#等待回复
        data = self.sockfd.recv(128).decode()if data  =='OK':
            data=self.sockfd.recv(4028).decode()print(data)return#退出defdo_quit(self):

        self.sockfd.send(b'Q')#请求退出
        self.sockfd.close()
        sys.exit('谢谢使用')#下载文件defdo_get(self,filename):
        self.sockfd.send(('G '+filename).encode())#等待回复
        data = self.sockfd.recv(128).decode()if data =='OK':
            f =open(filename,'wb')whileTrue:#循环接受写入文件
                data=self.sockfd.recv(1024)if data ==b'##':break

                f.write(data)
            f.close()defdo_put(self,filename):
        self.sockfd.send(('P '+filename).encode())#等待回复try:
           f=open(filename,'rb')except:print('文件不存在')return

        data = self.sockfd.recv(128).decode()if data =='OK':whileTrue:#循环发送文件
                data=f.read(1024)
                self.sockfd.send(data)ifnot data:
                    self.sockfd.send(b'##')print(f'{filename}传输完成')break
        f.close()#发送请求defmain():
    sockfd = socket()try:
        sockfd.connect(ADDR)except Exception as e:print(e)return
    ftp = FTPClient(sockfd)whileTrue:print('\n=======命令选项========')print('          list                ')print('          get file                ')print('          put file               ')print('          quit                ')
        cmd =input('输入命令:')if cmd.strip()=='list':print(1)
            ftp.do_list()elif cmd.strip()=='quit':
            ftp.do_quit()elif cmd[:3]=='get':
            filename = cmd.strip().split(' ')[-1]
            ftp.do_get(filename)elif cmd[:3]=='put':
            filename = cmd.strip().split(' ')[-1]
            ftp.do_put(filename)if __name__ =="__main__":
    main()
标签: 服务器 python 运维

本文转载自: https://blog.csdn.net/qq_42307546/article/details/132815668
版权归原作者 qq_42307546 所有, 如有侵权,请联系我们删除。

“文件服务器”的评论:

还没有评论