【问题标题】:trying to get info on boto docs试图获取有关 boto 文档的信息
【发布时间】:2015-07-17 11:01:37
【问题描述】:

我对 python 很陌生,并且正在研究 boto 来做一些 SG 的快速列表。我的问题是当我查看下面列出的 boto 文档时......我怎么知道我可以使用哪些属性。例如,在下面 python 的示例输出中,我创建了一个名为 inst 的变量......首先我用它来获取实例 ID,接下来我用它来获取 SG 信息......我的问题是...... . 我怎么知道我还能得到什么其他属性??也许是实例类型、ami-id 等。阅读下面的文档让我有点困惑,因为我是 python 新手...任何帮助/指针将不胜感激...

http://boto.readthedocs.org/en/latest/ref/ec2.html#module-boto.ec2.instance

>>> reservations = conn.get_all_instances()
>>> for i in reservations:
     inst = i.instances[0]
     print inst

Instance:i-c8990c39
Instance:i-c7e45537
Instance:i-698047c1
>>>
>>> for i in reservations:
     inst = i.instances[0].groups
     print inst

[<boto.ec2.group.Group object at 0xf2c7d0>]
[<boto.ec2.group.Group object at 0xf2c350>]
[<boto.ec2.group.Group object at 0xf2c750>
 <boto.ec2.group.Group object at 0x10992d0>]

另外,为什么我没有得到 SG,但我得到了实例 ID??

>>> for i in reservations:
      inst = i.instances
      print inst

[Instance:i-c8990c39]
[Instance:i-c7e45537]
[Instance:i-698047c1] 

>>>
>>> for i in reservations:
      sg = i.groups
      print sg

[] [] []
>>>
>>>

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto


    【解决方案1】:

    查看python内置函数dir: https://docs.python.org/2/library/functions.html#dir

    >>> reservations = conn.get_all_instances()
    >>> instance = reservations[0].instances[0]
    >>> dir(instance)
    ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_in_monitoring_element', '_placement', '_previous_state', '_state', '_update', 'add_tag', 'ami_launch_index', 'architecture', 'block_device_mapping', 'client_token', 'confirm_product', 'connection', 'create_image', 'dns_name', 'ebs_optimized', 'endElement', 'eventsSet', 'get_attribute', 'get_console_output', 'group_name', 'groups', 'hypervisor', 'id', 'image_id', 'instance_profile', 'instance_type', 'interfaces', 'ip_address', 'item', 'kernel', 'key_name', 'launch_time', 'modify_attribute', 'monitor', 'monitored', 'monitoring', 'monitoring_state', 'persistent', 'placement', 'placement_group', 'placement_tenancy', 'platform', 'previous_state', 'previous_state_code', 'private_dns_name', 'private_ip_address', 'product_codes', 'public_dns_name', 'ramdisk', 'reason', 'reboot', 'region', 'remove_tag', 'requester_id', 'reset_attribute', 'root_device_name', 'root_device_type', 'sourceDestCheck', 'spot_instance_request_id', 'start', 'startElement', 'state', 'state_code', 'state_reason', 'stop', 'subnet_id', 'tags', 'terminate', 'unmonitor', 'update', 'use_ip', 'virtualization_type', 'vpc_id']
    

    【讨论】:

      【解决方案2】:

      您向我们指出的EC2 instance reference是否包含属性列表,例如image_id(用于 AMI id)和 instance_type。您在寻找不同的东西吗?

      另外,请理解,当您“打印”一个对象时,您不一定会看到该对象的所有属性 - 相反,您经常会看到一个简单的字符串表示形式,例如“Instance:i-c8990c39”。

      最后,准备经常使用 dir(obj) 以了解有关 obj 属性的更多信息 - Python 文档很少(如果有的话)完整,而 dir(obj) 会帮助您。见how to use type, str, and dir for introspection

      【讨论】:

        【解决方案3】:

        如果您想打印出与您的实例关联的第一个安全组 ID,您可以执行以下操作:

        >>> reservations = conn.get_all_instances()
        >>>
        >>> for i in reservations:
             sg = i.instances[0].groups[0]
             print sg.id
        
        
        sg-9f1xxxxx
        sg-9f1xxxxx
        sg-9f1xxxxx
        sg-9f1xxxxx
        sg-9f1xxxxx
        sg-1faxxxxx
        sg-9f1xxxxx
        sg-9f1xxxxx
        sg-9f1xxxxx
        

        好的,我知道发生了什么,但还有一个愚蠢的问题: 为什么我没有得到 SG 但我得到了实例 id??

        >>> for i in reservations:
              inst = i.instances
              print inst
        
        [Instance:i-c8990c39]
        [Instance:i-c7e45537]
        [Instance:i-698047c1] 
        

        在这种情况下,您将打印所有预订。不是实例。每个预留都有一个实例。

        >>>
        >>> for i in reservations:
              sg = i.groups
              print sg
        
        [] [] []
        >>>
        >>>
        

        在这种情况下,您正在为保留打印安全组。预留没有与之关联的安全组。

        【讨论】:

        • 好吧,我知道发生了什么事,但另一个愚蠢的问题...为什么我没有得到 SG 但我得到了实例 ID? >>> for i in reservations: ... inst = i.instances ... print inst ... [Instance:i-c8990c39] [Instance:i-c7e45537] [Instance:i-698047c1] >>> >> > for i in reservations: ... sg = i.groups ... print sg ... [] [] [] >>> >>>
        • @maxscalf 检查我的答案。我添加了更多细节。这基本上是SO中的另一个问题,所以下次再问它。
        猜你喜欢
        • 1970-01-01
        • 2017-11-12
        • 2019-07-13
        • 1970-01-01
        • 1970-01-01
        • 2014-11-21
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        相关资源
        最近更新 更多