0

Python新手在这里。可以说我有这个:

def test_servers():
        env.user = getpass.getuser()
        env.hosts = []


我想做这样的事情:

def test_servers():
            env.user = getpass.getuser()
            system = raw_input("Enter FQDN to show (seperated by commas for multiple entries)> ")
            env.hosts = system.split(',')

??如何使它填充为:

env.hosts = ['firsthostinput', 'secondhostinput']

我仍在学习 python,我不确定系统拆分是否可以与其他东西结合使用来完成我希望它填充的格式。任何帮助将不胜感激。

编辑

这是我的工厂文件。只是做 system.split 是行不通的。而且由于一个特定的参数“审计”,我不能只在命令行上传递主机。它们必须在 input_servers() 中。

所以,这就是我的 fabfile.py 中的内容:

def input_servers():
    env.user = getpass.getuser()
    system = raw_input("Enter FQDN. User Commas to seperate multiple servers > "
    env.hosts = system.split(',')

如果我尝试运行它会发生这种情况:

    [me@mothership fab_files]$ fab input_servers audit
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/fabric/main.py", line 654, in main
    docstring, callables, default = load_fabfile(fabfile)
  File "/usr/lib/python2.6/site-packages/fabric/main.py", line 165, in load_fabfile
    imported = importer(os.path.splitext(fabfile)[0])
  File "/home/me/my-repo/stuff/fab_files/fabfile.py", line 17
    env.hosts = system.split(',')
      ^
SyntaxError: invalid syntax
4

1 回答 1

1
>>> system  = raw_input("Enter FQDN to show (seperated by commas for multiple entries)
> ")
Enter FQDN to show (seperated by commas for multiple entries)> apple,oranges,lim
es,lemons
>>> system 
'apple,oranges,limes,lemons'
>>> splitted = system .split(',')
>>> splitted
['apple', 'oranges', 'limes', 'lemons']

不太确定这里的问题是什么?

于 2013-04-30T18:07:21.473 回答