python-在临时文件夹上设置SFTP服务器?
是否可以在临时文件夹上设置SFTP(SSH文件传输协议)?我希望这个用于测试目的(首先设置SFTP,然后在测试后将其拆下) 或者,也许有一些模拟的方法,我可以模仿,好像SFTP存在于那个临时文件夹中 比如:python-在临时文件夹上设置SFTP服务器?,python,python-3.x,sftp,temporary-files,Python,Python 3.x,Sftp,Temporary Files,是否可以在临时文件夹上设置SFTP(SSH文件传输协议)?我希望这个用于测试目的(首先设置SFTP,然后在测试后将其拆下) 或者,也许有一些模拟的方法,我可以模仿,好像SFTP存在于那个临时文件夹中 比如: import tempfile import unittest class TestSFTP(unittest.TestCase): @classmethod def setupClass(cls): folder = tempfile.Temporar
import tempfile
import unittest
class TestSFTP(unittest.TestCase):
@classmethod
def setupClass(cls):
folder = tempfile.TemporaryDirectory()
# use temp folder and setup temp sftp to be used in tests
# define some test methods and use sftp.
@classmethod
def tearDownClass(cls):
# destroy temp sftp.
另外,通常要创建SFTP,您需要使用sudo,重新启动一些服务等,因此这种方法对于测试目的是不可行的
更新:
所以我能够设置测试类,它将运行sftp服务器,但当我需要正确地停止sftp服务器时,我遇到了问题。以下是我目前掌握的代码..:
import sftpserver
import paramiko
import os
import sh
import threading
from odoo.tests import common
from odoo.modules.module import get_module_path
def _start_thread(target, args=None, kwargs=None):
"""Run target object in thread, in a background."""
if not args:
args = []
if not kwargs:
kwargs = {}
thread = threading.Thread(target=target, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread
class TestExchangeCommon(common.SavepointCase):
"""Common class for exchange module tests."""
tests_path = os.path.join(get_module_path('exchange'), 'tests')
rsa_key_path = os.path.join(tests_path, 'extra/test_rsa.key')
@classmethod
def _start_sftp(cls):
sftpserver.start_server('localhost', 3373, cls.rsa_key_path, 'INFO')
@classmethod
def setUpClass(cls):
"""Set up data to be used by all test classes."""
import logging
_logger = logging.getLogger()
super(TestExchangeCommon, cls).setUpClass()
cls.thread = _start_thread(target=cls._start_sftp)
pkey = paramiko.RSAKey.from_private_key_file(cls.rsa_key_path)
cls.transport = paramiko.Transport(('localhost', 3373))
cls.transport.connect(username='admin', password='admin', pkey=pkey)
cls.sftp = paramiko.SFTPClient.from_transport(cls.transport)
_logger.warn(cls.sftp.listdir('.'))
@classmethod
def tearDownClass(cls):
"""Kill sftp server to stop running it in a background."""
cls.sftp.close()
cls.transport.close()
sh.fuser('-k', '3373/tcp') # this kills main application...
为了让sftp服务器运行,我必须加入一个线程,这样它就不会暂停我的主应用程序。但当测试完成后需要停止sftp服务器时,如果我在端口3373上终止(sftp服务器正在运行),它也会终止主应用程序,而主应用程序实际上在端口8069上运行。有没有办法通过python正确关闭sftpserver实例?下面是我的sftpserver安装实现,可以在unittests中使用 对于线程,停止sftpserver是有问题的,但是对于多处理,它似乎可以正常工作 还请注意,我在进程启动后添加了睡眠,因为至少在我的情况下,有时sftp服务器无法及时启动,我会收到一个错误,连接被拒绝 (但pkey和sftpserver位置的实现路径在不同的应用程序中可能不同):
谷歌@AntiMatterDynamite的第一件事就是谢谢你,我会看看的。不知怎的,我没有找到它。也许我的搜索太具体了……)
import sftpserver
import paramiko
import os
import sh
import multiprocessing
import time
from odoo.tests import common
from odoo.modules.module import get_module_path
def _start_process(target, args=None, kwargs=None):
"""Run target object in new process, in a background."""
if not args:
args = []
if not kwargs:
kwargs = {}
process = multiprocessing.Process(target=target, args=args, kwargs=kwargs)
process.daemon = True
process.start()
return process
class TestExchangeCommon(common.SavepointCase):
"""Common class for exchange module tests."""
tests_path = os.path.join(get_module_path('exchange'), 'tests')
rsa_key_path = os.path.join(tests_path, 'extra/test_rsa.key')
@classmethod
def _start_sftp(cls):
sftpserver.start_server('localhost', 3373, cls.rsa_key_path, 'INFO')
@classmethod
def setUpClass(cls):
"""Set up data to be used by all test classes."""
import logging
_logger = logging.getLogger()
super(TestExchangeCommon, cls).setUpClass()
cls.process = _start_process(target=cls._start_sftp)
time.sleep(0.01) # give time for server to start up.
pkey = paramiko.RSAKey.from_private_key_file(cls.rsa_key_path)
cls.transport = paramiko.Transport(('localhost', 3373))
cls.transport.connect(username='admin', password='admin', pkey=pkey)
cls.sftp = paramiko.SFTPClient.from_transport(cls.transport)
_logger.warn(cls.sftp.listdir('.'))
@classmethod
def tearDownClass(cls):
"""Kill sftp server to stop running it in a background."""
cls.sftp.close()
cls.transport.close()
sh.fuser('-k', '3373/tcp')