合并sf代码

This commit is contained in:
gqh
2023-01-11 14:51:59 +08:00
parent aba5a076b1
commit 8e51654f17
23 changed files with 547 additions and 621 deletions

View File

@@ -2,13 +2,18 @@
import os
from ftplib import FTP
class FTP_P(FTP):
"""
重写FTP类重写dirs方法
"""
def dirs(self, *args):
'''List a directory in long form.
"""List a directory in long form.
By default list current directory to stdout.
Optional last argument is callback function; all
non-empty arguments before it are concatenated to the
LIST command. (This *should* only be used for a pathname.)'''
LIST command. (This *should* only be used for a pathname.)"""
cmd = 'LIST'
templist = []
tempdic = {}
@@ -19,7 +24,6 @@ class FTP_P(FTP):
if arg:
cmd = cmd + (' ' + arg)
self.retrlines(cmd, templist.append)
# print(templist)
# 处理返回结果,只需要目录名称
r_files = [file.split(" ")[-1] for file in templist]
tempdic['name'] = [file for file in r_files if file != "." and file != ".."]
@@ -27,13 +31,14 @@ class FTP_P(FTP):
return tempdic
# return [file for file in r_files if file != "." and file != ".."]
# FTP接口类
class FtpController():
'''
# FTP接口类
class FtpController:
"""
这是ftp接口类在类初始化的时候就连接了ftp服务器能否成功连接有反馈。
类中定义了两个接口:上传接口和删除接口
'''
"""
# 三菱机床连接
def __init__(self, host="192.168.2.158", port=8080, username="MITSUBISHI", password="CNC"):
self.host = host
@@ -42,47 +47,41 @@ class FtpController():
self.password = password
ftp = FTP_P()
# self.ftp.set_debuglevel(2) #打开调试级别2显示详细信息
ftp.set_pasv(0) #0主动模式 1 #被动模式
ftp.set_pasv(0) # 0主动模式 1 #被动模式
try:
ftp.connect(self.host, self.port)
ftp.login(self.username, self.password)
print("连接成功")
self.ftp = ftp
except:
print("连接失败")
except Exception as e:
print("连接失败" + str(e))
# 试验接口
def prin(self):
print("这是试验接口")
# 试验ftp服务器连接
# def connect(self, host="127.0.0.1", port=2121, username="admin", password="123456"):
# ftp = FTP_P()
# try:
# ftp.connect(host, port)
# ftp.login(username, password)
# print("连接成功")
# ftp1 = ftp
# return ftp1
# except:
# print("连接失败")
# 三菱代码下发
def upload_file(self, remotepath='/(192,168,199,2)/DS/Z4.5.NC', localpath='D:/ftp/up/Z4.5.NC'):
'''
"""
第一个是要上传到ftp服务器路径下的文件第二个是本地要上传的的路径文件
:param remotepath: 上传和下载都需要设置工作目录,注意只能使用文件名,不能有路径中的冒号
:param localpath:
:return:
'''
"""
bufsize = 1024
fp = open(localpath, 'rb')
self.ftp.storbinary('STOR ' + remotepath, fp, bufsize)
fp.close()
# return "上传成功"
# fp = open(localpath, 'rb')
# self.ftp.storbinary('STOR ' + remotepath, fp, bufsize)
# fp.close()
with open(localpath, mode='rb') as file:
self.ftp.storbinary('STOR ' + remotepath, file, bufsize)
def delAllfile(self, ftppath):
"""
删除ftp服务器端全部文件
:param ftppath:
:return:
"""
dir_res = []
try:
print(ftppath)
@@ -106,13 +105,13 @@ class FtpController():
# print("删除FTP目录" + ftppath + "下存在文件:" + f)
# ftp.delete(f)
except Exception as e:
raise e
print("删除失败" + str(e))
# 删除远端ftp文件
# 出现550 not found file是路径不对
# def del_file(self, delpath='./YIN.NC'):
def del_file(self, delpath='/(192,168,199,2)/DS/Z4.5.NC'):
self.ftp.delete(delpath) # 删除远程文件
"""
删除ftp服务器端指定文件
:param delpath:
:return:
"""
self.ftp.delete(delpath)