本文整理汇总了Python中airflow.contrib.hooks.ssh_hook.SSHHook.tunnel方法的典型用法代码示例。如果您正苦于以下问题:Python SSHHook.tunnel方法的具体用法?Python SSHHook.tunnel怎么用?Python SSHHook.tunnel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类airflow.contrib.hooks.ssh_hook.SSHHook
的用法示例。
在下文中一共展示了SSHHook.tunnel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SSHHookTest
# 需要导入模块: from airflow.contrib.hooks.ssh_hook import SSHHook [as 别名]
# 或者: from airflow.contrib.hooks.ssh_hook.SSHHook import tunnel [as 别名]
class SSHHookTest(unittest.TestCase):
def setUp(self):
configuration.test_mode()
from airflow.contrib.hooks.ssh_hook import SSHHook
self.hook = SSHHook()
self.hook.no_host_key_check = True
def test_remote_cmd(self):
output = self.hook.check_output(["echo", "-n", "airflow"])
self.assertEqual(output, b"airflow")
def test_tunnel(self):
print("Setting up remote listener")
import subprocess
import socket
self.handle = self.hook.Popen([
"python", "-c", '"{0}"'.format(HELLO_SERVER_CMD)
], stdout=subprocess.PIPE)
print("Setting up tunnel")
with self.hook.tunnel(2135, 2134):
print("Tunnel up")
server_output = self.handle.stdout.read(5)
self.assertEqual(server_output, b"ready")
print("Connecting to server via tunnel")
s = socket.socket()
s.connect(("localhost", 2135))
print("Receiving...",)
response = s.recv(5)
self.assertEqual(response, b"hello")
print("Closing connection")
s.close()
print("Waiting for listener...")
output, _ = self.handle.communicate()
self.assertEqual(self.handle.returncode, 0)
print("Closing tunnel")