python
#ftpserver.py
from socket import *
from threading import Thread
import os,time
#全局变量
HOST = '0.0.0.0'
PORT=8080
ADDR = (HOST,PORT)
FTP = "FTP/" #文件库位置
#创建类实现服务器文件处理功能
class FTPServer(Thread):
"""
查看列表,下载,上传,退出
"""
def __init__(self,connfd):
self.connfd = connfd
super().__init__()
def do_list(self):
files = os.listdir(FTP)
if not files:
self.connfd.send('文件库为空'.encode())
return
else:
self.connfd.send(b'OK')
time.sleep(0.1)
#拼接文件
filelist=''
for file in files:
if file[0] != '.' and \
os.path.isfile(FTP+file):
filelist +=file+'\n'
self.connfd.send(filelist.encode())
def do_get(self,filename):
try:
f = open(FTP+filename,'rb')
except Exception:
self.connfd.send('文件不存在'.encode())
return
else:
self.connfd.send(b'OK')
time.sleep(0.1)
while True:
data = f.read(1024)
if not data:
time.sleep(0.1)
self.connfd.send(b'##')
return
self.connfd.send(data)
def do_put(self, filename):
if os.path.exists(FTP+filename):
self.connfd.send('文件已经存在'.encode())
return
else:
self.connfd.send(b'OK')
f = open(FTP+filename,'wb')
while True:
data = self.connfd.recv(1024)
if data == b'##':
break
f.write(data)
f.close()
#打开失败文件不存在
#循环接受请求,分情况调用功能函数
def run(self):
while True:
data = self.connfd.recv(1024).decode()
if not 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)
#搭建网络服务端模型
def main():
s = socket()
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.bind(ADDR)
s.listen(5)
# 处理僵尸进程
while True:
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
python
"""
ftp 文件服务,客户端
"""
import sys
from socket import *
#服务器地址
ADDR = ('192.168.87.128',8080)
#客户端文件处理类
class FTPClient:
def __init__(self,sockfd):
self.sockfd = sockfd
def do_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
#退出
def do_quit(self):
self.sockfd.send(b'Q')#请求退出
self.sockfd.close()
sys.exit('谢谢使用')
#下载文件
def do_get(self,filename):
self.sockfd.send(('G '+filename).encode())
#等待回复
data = self.sockfd.recv(128).decode()
if data == 'OK':
f = open(filename,'wb')
while True:
#循环接受写入文件
data=self.sockfd.recv(1024)
if data == b'##':
break
f.write(data)
f.close()
def do_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':
while True:
#循环发送文件
data=f.read(1024)
self.sockfd.send(data)
if not data:
self.sockfd.send(b'##')
print(f'{filename}传输完成')
break
f.close()
#发送请求
def main():
sockfd = socket()
try:
sockfd.connect(ADDR)
except Exception as e:
print(e)
return
ftp = FTPClient(sockfd)
while True:
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()