此代码在linux上编写,适用于linux,windows下需要更改几个命令。
1、客户端输入IP,端口,可服务器端进行连接,被要求输入用户名和密码进行验证。
2、使用独立的模块来验证登录用户(技术有限,不支持客户端创建用户),用户名:ftpuser 密码:userlogin
2、客户端登录验证成功后,可使用?或者help查看可使用的命令。
ftpserver.py
复制代码 代码如下:
"Program for ftp server"
from SocketServer import
from time import
import os
import loginauth
class MyFtp(StreamRequestHandler):
def handle(self):
try:
while True:
sleep(0.5)
self.request.sendall('auth')
name = self.request.recv(BUFSIZ)
sleep(0.5)
self.request.sendall('pauth')
password = self.request.recv(BUFSIZ)
print name,password
auth_result = loginauth.user_create(name,password)
print auth_result
if auth_result == 0:
self.request.sendall('ok2login')
break
elif auth_result == 1:
self.request.sendall('fail2login')
continue
while True:
recv_data = self.request.recv(BUFSIZ).split()
if recv_data[0] == 'rls':
result = os.popen('ls -l ./').read()
self.request.sendall(result)
continue
if recv_data[0] == '?' or recv_data[0] == 'help':
send_help = '''\033[32;1m
?\help: Get help.
Get: Downlaod file from remote server.
Send: Send local file to remote server.
ls: List local file.
rls: List remote server file.
quit\exit: Quit the application.
\033[0m'''
self.request.sendall(send_help)
continue
if recv_data[0] == 'send':
filename = recv_data[1]
self.request.sendall('ok2send')
recv_data = self.request.recv(BUFSIZ)
file2w = open(filename,'wb')
file2w.write(recv_data)
file2w.flush()
file2w.close()
self.request.sendall('\033[33;1mFile transfer successed!!!\033[0m')
continue
if recv_data[0] == 'get':
filename = recv_data[1]
if os.path.isfile(filename):
self.request.sendall('ok2get')
if self.request.recv(BUFSIZ) == 'ok2send':
self.request.sendall('sending')
sleep(0.5)
file_data = open(filename,'rb')
file_tmp = file_data.read()
self.request.sendall(file_tmp)
sleep(1)
self.request.sendall('\033[33;1mDownloading complete!\033[0m')
file_data.close()
else:
self.request.sendall('fail2get')
if self.request.recv(BUFSIZ) == 'ack':
self.request.sendall('\033[31;1m%s not found\033[0m'% filename)
except :
pass
if name == 'main':
HOST,PORT = '',9889
ADDR = (HOST,PORT)
BUFSIZ = 8192
try:
server = ThreadingTCPServer(ADDR,MyFtp)
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8