1
0
mirror of https://gitlab.com/libvirt/libvirt-python.git synced 2025-07-06 12:59:32 +03:00

examples: Fix connection error handling

libvirt.open*() does not return None but raises an exception

Signed-off-by: Philipp Hahn <hahn@univention.de>
This commit is contained in:
Philipp Hahn
2020-04-20 08:05:00 +02:00
committed by Daniel P. Berrangé
parent 53565fe096
commit 283e2bc693
5 changed files with 15 additions and 11 deletions

View File

@ -34,8 +34,9 @@ if len(sys.argv) != 2:
name = sys.argv[1]
# Connect to libvirt
conn = libvirt.openReadOnly(None)
if conn is None:
try:
conn = libvirt.openReadOnly(None)
except libvirt.libvirtError:
print('Failed to open connection to the hypervisor')
sys.exit(1)

View File

@ -20,8 +20,9 @@ if len(sys.argv) != 2:
dir = sys.argv[1]
imgs = os.listdir(dir)
conn = libvirt.open(None)
if conn is None:
try:
conn = libvirt.open(None)
except libvirt.libvirtError:
print('Failed to open connection to the hypervisor')
sys.exit(1)

View File

@ -18,8 +18,9 @@ if len(sys.argv) != 2:
dir = sys.argv[1]
conn = libvirt.open(None)
if conn is None:
try:
conn = libvirt.open(None)
except libvirt.libvirtError:
print('Failed to open connection to the hypervisor')
sys.exit(1)

View File

@ -32,8 +32,9 @@ if len(sys.argv) != 2:
(name, xmldesc) = read_domain(sys.argv[1])
conn = libvirt.open(None)
if conn is None:
try:
conn = libvirt.open(None)
except libvirt.libvirtError:
print('Failed to open connection to the hypervisor')
sys.exit(1)

View File

@ -97,9 +97,9 @@ uri = "esx://%s/?no_verify=1" % hostname
# in order to log into the vCenter
auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
request_credentials, None]
conn = libvirt.openAuth(uri, auth, 0)
if conn is None:
try:
conn = libvirt.openAuth(uri, auth, 0)
except libvirt.libvirtError:
print("Failed to open connection to %s" % hostname)
sys.exit(1)