简体   繁体   中英

Why libvirt python module stopping my script?

So, I'm in process of learning python with libvirt module. Here is a little script that I made which checks if connection with libvirtd is successfully established and checks for one domain. I'm not developer and I'm taking some shortcuts so I don't understand how python or libvirt module works. But my real problem at this moment why is my script closing if connection is not established or domain is not found.

    #!/usr/bin/env python3
    from __future__ import print_function
    import sys
   import libvirt

   domName = 'server1'

   conn = libvirt.open('qemu:///system')
   if conn == None:
        print('Failed to open connection to qemu:///system', file=sys.stderr)
        exit(1)
   else:
        print('Connection opened sucessfully')

   dom = conn.lookupByName(domName)
   if dom == None:
        print('Failed to find the domain '+domName, file=sys.stderr)
        exit(1)
   else:
        print('Domain '+domName+' was found')

   conn.close()
        exit(0)

For example libvirtd service is stopped and connection is not established and instead going further down the lines into if statement it just prints some errors and stops, so there is an if statement which should check for this, but like this it does not has any functionality. It looks like this

[root@localhost Documents]# ./virt.py 
libvirt: XML-RPC error : Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
Traceback (most recent call last):
  File "./virt.py", line 11, in <module>
    conn = libvirt.open('qemu:///system')
  File "/usr/local/lib64/python3.6/site-packages/libvirt.py", line 277, in open
    if ret is None:raise libvirtError('virConnectOpen() failed')
libvirt.libvirtError: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
[root@localhost Documents]#  

I managed to suppress errors but then it just the same thing but without errors. Also I found this script here .

I found this script here (...)

Well, then you've learned one first lesson: you shouldn't rely on copy-pasting code found "here" (nor in most other places actually). Actually, you can consider that about 80% of the code you'll find on the net is crap (and I'm being generous).

I'm in process of learning python

Did you do the full official Python tutorial ? If no, then that's really what you want to start with (assuming you already get the basic concepts like types, variables, conditionals, iteration, functions, etc - else you want this instead)

For example libvirtd service is stopped and connection is not established and instead going further down the lines into if statement it just prints some errors and stops

Like most modern languages, Python uses a mechanism named "exceptions" to signal errors. This is much more powerful, usable and reliable than returning error codes or special values from functions...

This is all explained in the tutorial , so I won't bother posting corrected code, just following the tutorial should be enough to let you fix this code by yourself.

libvirt.libvirtError: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory

This error message suggests that the libvirtd daemon is not actually running on this host.

Your script still needs changes though if you want to catch errors. libvirt APIs will raise exceptions when things go wrong, so instead of checking the return value against "None", you need a try/except block to catch & handle it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM