Show Table of Contents
2.2. 示例:使用 Python 访问 API 的进入点
ovirtsdk Python 库提供了
API 类,它被作为 API 的进入点使用。
例 2.1. 使用 Python 访问 API 的进入点
这个 python 例子连接到 Red Hat Virtualization Manager(位于
rhevm.demo.redhat.com)所提供的一个 REST API 实例(instance)上。如果连接成功,它会创建一个 API 类。如果需要断开连接,使用 API 类的 disconnect() 方法。
在这个示例中,提供给
API 类的构造函数的参数包括:
- 要连接到的 Manager 的
url。 - 用来进行用户验证的用户的
username。 - 用来进行用户验证的用户的
password。 ca_file(到证书的路径)。证书是 Manager 的证书颁发机构所颁发的文件,它可以通过https://[engine-fqdn]ovirt-engine/services/pki-resource?resource=ca-certificate&format=X509-PEM-CA获得。
API 类的构造函数还支持其它参数。这个示例中只包括了必需的参数。
from ovirtsdk.api import API
from ovirtsdk.xml import params
try:
api = API (url="https://HOST",
username="USER@DOMAIN",
password="PASS",
ca_file="ca.crt")
print "Connected to %s successfully!" % api.get_product_info().name
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % ex
如果连接成功,这个示例会显示以下信息:
Connected to Red Hat Enterprise Virtualization Manager successfully!

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.