如何在Django中调用远程Fabric方法 [英] how to call a remote fabric method in django
本文介绍了如何在Django中调用远程Fabric方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过django在远程计算机上调用Fabric方法.我的意思是,当用户发送给定请求时,将获得远程计算机的主机名. 像这样:
I need to call a fabric method on a remote machine through django. I mean when user send a given request get the hostname of a remote machine. Something like this:
def get_hostname(request):
hostname = os.system('fab remote_server hostname')
return hostname
推荐答案
为获得更好的控制和灵活性,您应该将Fabric用作库.请参阅: http://docs.fabfile.org/en/1.3.3 /usage/library.html
For greater control and flexibility you should use fabric as a library. see: http://docs.fabfile.org/en/1.3.3/usage/library.html
import fabric.api as fab
from fabric.network import disconnect_all
from contextlib import contextmanager
@context_manager
def ssh(settings):
with settings:
try:
yield
finally:
disconnect_all()
def hostname(request, host='somehost', user='someuser', pw='secret'):
with ssh(fab.settings(host_string=host, user=user, password=pw)):
return fab.run('hostname')
这篇关于如何在Django中调用远程Fabric方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文