title: Why libvirt python module stopping my script?
body: <p>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.</p>
  
  <pre><code>    #!/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)
  </code></pre>
  
  <p>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</p>
  
  <pre><code>[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 &lt;module&gt;
      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]#  
  </code></pre>
  
  <p>I managed to suppress errors but then it just the same thing but without errors. Also I found this script <a href="https://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/libvirt_application_development_guide_using_python-Guest_Domains-Information-Info.html" rel="nofollow noreferrer">here</a>.</p>