简体   繁体   English

为什么 libvirt python 模块停止我的脚本?

[英]Why libvirt python module stopping my script?

So, I'm in process of learning python with libvirt module.所以,我正在用 libvirt 模块学习 python。 Here is a little script that I made which checks if connection with libvirtd is successfully established and checks for one domain.这是我制作的一个小脚本,用于检查与 libvirtd 的连接是否成功建立并检查一个域。 I'm not developer and I'm taking some shortcuts so I don't understand how python or libvirt module works.我不是开发人员,我正在走一些捷径,所以我不明白 python 或 libvirt 模块是如何工作的。 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.例如,libvirtd 服务已停止且未建立连接,而是进一步深入到 if 语句中,它只是打印一些错误并停止,因此有一个 if 语句应该检查这一点,但像这样它没有任何功能. 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).实际上,您可以认为您在网上找到的大约 80% 的代码都是垃圾(而且我很慷慨)。

I'm in process of learning python我正在学习python

Did you do the full official Python tutorial ?你做了完整的官方 Python 教程吗? 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例如,libvirtd 服务已停止且未建立连接,而是进一步深入到 if 语句中,它只是打印一些错误并停止

Like most modern languages, Python uses a mechanism named "exceptions" to signal errors.像大多数现代语言一样,Python 使用一种名为“异常”的机制来表示错误。 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.此错误消息表明libvirtd守护程序实际上并未在此主机上运行。

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 libvirt API 会在出现问题时引发异常,因此您需要一个 try/except 块来捕获和处理它,而不是根据“None”检查返回值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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