python模拟简单ftp

简介:

需求:开发简单的FTP

  1. 用户登陆
  2. 上传/下载文件
  3. 不同用户家目录不同
  4. 查看当前目录下文件
  5. 充分使用面向对象知识
    代码目录结构:
    python模拟简单ftp
    流程图:
    python模拟简单ftp
    客户端代码:
    
    import os,sys
    import getpass
    import platform
    if platform.system() == "Windows":
    BASE_DIR = "\\".join(os.path.abspath(os.path.dirname(__file__)).split("\\")[:-1])
    else:
    BASE_DIR = "/".join(os.path.abspath(os.path.dirname(__file__)).split("/")[:-1])
    sys.path.insert(0,BASE_DIR)
    from modules import ssh

#print(sys.path)

if name == "main":
#host = input("请输入服务器端IP:").strip()
#port = input("请输入端口:").strip()
username = input("请输入用户名:").strip()
password = input("请输入密码:").strip()
#password = getpass.getpass("请输入密码:").strip()
ssh.SSH("host","port",username,password)

服务端代码:

import os,sys
import socket
import platform

if platform.system() == "Windows":
BASE_DIR = "\".join(os.path.abspath(os.path.dirname(file)).split("\")[:-1])
else:
BASE_DIR = "/".join(os.path.abspath(os.path.dirname(file)).split("/")[:-1])
sys.path.insert(0,BASE_DIR)
from modules import ftp

#从文件获取用户账号信息
def get_acount():
acount_dict = {}
f = open(BASE_DIR+"\conf\acount.conf")
for account in f:
acount_dict[account.split()[0]] = account.split()[1]
return acount_dict

if name == "main":
server = socket.socket()
server.bind(("localhost",10620))
server.listen(5)
print("准备接受连接.....")
while True:
conn, addr = server.accept()
auth = conn.recv(1024)
username = auth.decode("utf-8").split()[0]
password = auth.decode("utf-8").split()[1]
acount = get_acount()
if username in acount and password == acount[username]:
conn.send(b"success")
if os.path.exists(("C:\Users\%s" %username)):
home_dir = "C:\Users\%s" %username
conn.send(home_dir.encode("utf-8"))
else:
os.mkdir("C:\Users\%s" %username)
conn.send(home_dir)
os.chdir(home_dir)
conn.send("登录成功!命令帮助请输入h或?".encode("utf-8"))
help = '''
put [file_name] 上传本地文件到ftp服务器。例如:put file.txt
get [file_name] 下载ftp服务器文件到本地。例如:get file.txt
command 执行操作系统命令。例如:dir ipconfig
quit|exit|q 退出登录。例如:q
'''
while True:
command = conn.recv(1024).decode("utf-8")
if command == "?" or command == "h":
conn.send(help.encode("utf-8"))
elif command.split()[0] == "get":# or command.split()[0] == "put": #判断用户是否执行ftp命令
if len(command.split()) != 2:
conn.send(("%s命令格式为:%s FileName" %(command.split()[0],command.split()[0])).encode("utf-8"))
continue
else:
Ftp = ftp.FTP(conn,command.split()[0],command.split()[1]) #调用FTP模块传输文件
Ftp.download()
elif command.split()[0] == "put": # 判断用户是否执行ftp命令
f = open(command.split()[1], "wb")
fsize = conn.recv(1024).decode("utf-8")
conn.send(b"ok")
while True:
data = conn.recv(102400)
f.write(data)
f.flush()
if os.path.getsize(command.split()[1]) == int(fsize):
break
f.close()
elif command == "q" or command == "quit" or command == "exit":
conn.send("q".encode("utf-8"))
conn.close()
break
else:
res = os.popen(command).read()
if len(res) == 0: #如果是不存在的系统命令,则提醒用户输入错误
conn.send(("%s:command not found" % command).encode("utf-8"))
else: #以上条件都不符合后执行此步骤,此块内容为执行系统命令
conn.sendall(res.encode("utf-8"))
continue
else:
print(2222)
conn.close()
continue
server.close()

ssh模块:

import os,sys
import socket
import platform
if platform.system() == "Windows":
BASE_DIR = "\".join(os.path.abspath(os.path.dirname(file)).split("\")[:-1])
else:
BASE_DIR = "/".join(os.path.abspath(os.path.dirname(file)).split("/")[:-1])
sys.path.insert(0,BASE_DIR)
from modules import ftp

class SSH(object):
def init(self,host,port,username,password):
client = socket.socket()
self.host = host
self.port = port
self.username = username
self.password = password
#client.connect((self.host,int(self.port)))
client.connect(("localhost",10620))
#登录时将账号发送到服务端进行验证,验证通过后进入循环命令输入
#client.send((self.username+" "+self.password).encode("utf-8"))
client.send(("zhaohh" + " " + "123").encode("utf-8"))
auth_res = client.recv(1024)
if auth_res.decode("utf-8") == "success":
home_dir = client.recv(1024) #获取用户登录成功后的家目录
welcom = client.recv(1024)
print(welcom.decode("utf-8"))
while True:
command = input("[%s]$ "%home_dir.decode("utf-8")).strip()
if len(command) == 0:continue
client.send(command.encode("utf-8"))
if command.split()[0] == "get":
f = open(command.split()[1],"wb")
fsize = client.recv(1024).decode("utf-8")
client.send(b"ok")
while True:
data = client.recv(102400)
f.write(data)
f.flush()
if os.path.getsize(command.split()[1]) == int(fsize):
break
f.close()
elif command.split()[0] == "put":
if len(command.split()) != 2:
print("%s命令格式为:%s FileName" % (command.split()[0], command.split()[0]))
continue
else:
Ftp = ftp.FTP(client, command.split()[0], command.split()[1]) # 调用FTP模块传输文件
Ftp.upload()
else:
res = client.recv(102400)
if res.decode("utf-8") == "q":
exit("已退出登录!")
else:
print(res.decode())
else:
input("账号错误!")

ftp模块:

import os
class FTP(object):
def init(self,conn,command,filename):
self.command = command
self.filename = filename
self.conn = conn

#上传文件
def upload(self):
    print("上传文件:%s" %self.filename)
    f = open(self.filename,"rb")
    data = f.read()
    fsize = os.path.getsize(self.filename)
    self.conn.send(str(fsize).encode("utf-8"))
    self.conn.recv(1024)
    self.conn.sendall(data)

#下载文件
def download(self):
    f = open(self.filename,"rb")
    data = f.read()
    fsize = os.path.getsize(self.filename)
    self.conn.send(str(fsize).encode("utf-8"))
    self.conn.recv(1024)
    self.conn.sendall(data)

用户账号文件内容格式:
zhaohh 123
alex 123




本文转自 baiying 51CTO博客,原文链接:http://blog.51cto.com/baiying/2055011,如需转载请自行联系原作者
目录
相关文章
|
1天前
|
Shell Python Windows
通过Python实现win11环境下FTP的上传与下载
通过Python实现win11环境下FTP的上传与下载
|
Python
Python:利用蒙特卡洛方法模拟验证概率分布
这个题目可以使用数学方法,将其答案显式地写出来,但是验证解出来的答案是否正确,就可以使用蒙特卡洛方法了。
363 0
Python:利用蒙特卡洛方法模拟验证概率分布
|
12月前
|
存储 数据安全/隐私保护 Python
用python写一款FTP自动化的脚本
用python写一款FTP自动化的脚本
193 0
|
编解码 数据安全/隐私保护 Python
Python操作FTP服务器实现文件和文件夹的上传与下载,python清理ftp目录下的所有文件和非空文件夹
Python操作FTP服务器实现文件和文件夹的上传与下载,python清理ftp目录下的所有文件和非空文件夹
189 0
|
编解码 数据安全/隐私保护 Python
Python 连接FTP服务器并实现文件夹下载实例演示,python区分ftp目录下文件和文件夹方法,ftp目录下包含中文名问题处理
Python 连接FTP服务器并实现文件夹下载实例演示,python区分ftp目录下文件和文件夹方法,ftp目录下包含中文名问题处理
178 0
|
Web App开发 jenkins 测试技术
web自动化 基于python+Selenium+PHP+Ftp实现的轻量级web自动化测试框架
web自动化 基于python+Selenium+PHP+Ftp实现的轻量级web自动化测试框架
150 0
|
Linux 测试技术 Python
Python 基于Python实现Ftp文件上传,下载
Python 基于Python实现Ftp文件上传,下载
387 0
|
存储 程序员 Linux
python 使用ftplib连接ftp服务器获取目录、文件及它们的修改时间
* 获取当前路径或者指定路径下的文件、目录 * 检查指定路径是目录还是文件 * 根据目录、文件的修改时间来判断是否下载ftp的文件。 由于ftplib中的FTP无法满足我这一需求,所以只能重写一个MyFTP类继承FTP,写一个方法来实现,除了这个还实现了一个获取当前目录下的所有目录及文件。
1100 0
python 使用ftplib连接ftp服务器获取目录、文件及它们的修改时间
python--模拟掷骰子游戏
通过python模拟掷骰子的游戏
python--模拟掷骰子游戏
|
算法 安全 PHP
【高级软件实习】蒙特卡洛模拟 | PRNG 伪随机数发生器 | LCG 线性同余算法 | 马特赛特旋转算法 | Python Random 模块
本篇博客将介绍经典的伪随机数生成算法,我们将 重点讲解 LCG(线性同余发生器) 算法与马特赛特旋转算法,在此基础上顺带介绍 Python 的 random 模块。 本篇博客还带有练习,无聊到喷水的练习,咳咳…… 学完前面的内容你就会了解到 Python 的 Random 模块的随机数生成的实现,是基于马特赛特旋转算法的,比如 random_uniform 函数。而本篇博客提供的练习会让你实现一个基于 LCG 算法的random_uniform,个人认为还是比较有意思的
475 0
【高级软件实习】蒙特卡洛模拟 | PRNG 伪随机数发生器 | LCG 线性同余算法 | 马特赛特旋转算法 | Python Random 模块