Merge branch 'feature/从ftp获取cnc程序文件' into develop

# Conflicts:
#	sf_manufacturing/models/mrp_workorder.py
This commit is contained in:
jinling.yang
2022-11-30 17:27:47 +08:00
5 changed files with 89 additions and 12 deletions

View File

@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
import os
import posixpath
from odoo.modules import get_resource_path
from ftplib import FTP
import logging
# FTP接口类
class FtpController():
'''
这是ftp接口类在类初始化的时候就连接了ftp服务器能否成功连接有反馈。
类中定义了两个接口:上传接口和删除接口
'''
ftp = FTP()
def __init__(self, host="192.168.50.202", port=21, username="ftpuser", password="123456"):
try:
self.ftp.connect(host, port)
self.ftp.login(username, password)
except:
logging.info("连接失败: ")
# 下载目录下的文件
def download_file_tree(self, remotepath, serverdir):
if not os.path.exists(serverdir):
os.makedirs(serverdir)
self.ftp.cwd(remotepath)
remotenames = self.ftp.nlst()
for file in remotenames:
server = os.path.join(serverdir, file)
if file.find(".") != -1:
self.download_file(server, file)
else:
return False
# 下载指定目录下的指定文件
def download_file(self, serverfile, remotefile):
file_handler = open(serverfile, 'wb')
self.ftp.retrbinary('RETR ' + remotefile, file_handler.write)
file_handler.close()