Show Table of Contents
第 3 章 使用软件开发套件
3.1. 使用 Python 连接 API
要使用 Python 连接到 REST API,您需要从 ovirtsdk.api 模块中创建一个
API 类的实例(instance)。要实现这个任务,需要在脚本的开始部分首先导入这个类。
from ovirtsdk.api import API
API 类的构造函数支持以下参数:
- url
- 指定要连接到的 Manager 的 URL(包括
/api路径)。这个参数是必需的。 - username
- 指定连接时使用的用户名(使用 UPD 格式)。这个参数是必需的。
- password
- 指定
username参数中指定的用户的密码。这个参数是必需的。 - Kerberos
- 使用有效 Kerberos ticket 验证连接。有效值为
True和False。这个参数是自选的。 - key_file
- 指定一个 PEM 格式的密钥文件。这个密钥文件包括了
cert_file指定的证书的私人密钥。这个参数是可选的。 - cert_file
- 指定一个 PEM 格式的客户端证书。这个证书被用来在服务器上建立客户端的身份。这个选项是可选的。
- ca_file
- 指定服务器证书颁发机构的证书文件。除非
insecure参数被设置为True,这个参数是必需的。 - port
- 指定连接使用的端口(当
url参数没有提供相应信息时有效)。这个参数是可选的。 - timeout
- 一个请求的超时时间(以秒为单位)。这个选项是可选的。
- persistent_auth
- 指定是否为连接建立持久性验证。有效值是
True和False。这个参数是可选的,默认值是False。 - insecure
- 允许通过没有证书颁发机构的 SSL 进行连接。有效值包括
True和False。如果insecure参数被设置为False(默认的值),必须在连接时提供ca_file来建立安全的连接。在使用这个选项时需要格外小心。如果使用不当,可能会被"中间人攻击“所利用来进行服务器身份欺骗。 - filter
- 指定是否启用基于用户权限的过滤器。有效值是
True和False。如果filter参数被设置为False(默认值),所使用的用户验证信息必须是管理员用户。如果filter参数被设置为True,则任何用户都可以使用,Manager 会根据用户的权限决定用户可以进行什么操作。 - debug
- 指定这个连接是否启用调试(debug)模式。有效值是
True和False。这个参数是可选的。
您可以通过创建并使用多个 ovirtsdk.API Python 项实例来和多个 Red Hat Virtualization Managers 进行交流。
这个示例脚本创建一个
API 项实例,使用 test() 方法检查连接是否工作正常,然后使用 disconnect() 方法断开连接。
from ovirtsdk.api import API
api_instance = API ( url="https://rhevm31.demo.redhat.com",
username="admin@internal",
password="Password",
ca_file="/etc/pki/ovirt-engine/ca.pem")
print "Connected successfully!"
api_instance.disconnect()
有关
API 项支持方法的完整列表请参考 ovirtsdk.api 模块的 pydoc 输出结果。
$ pydoc ovirtsdk.api

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.