使用libvirt抓取linux kvm虚拟机的缩略图,libvirtkvm,import libvi


import libvirtimport osimport uuidtry:    from PIL import Image    print("PIL")except ImportError:    import Imagedef handler(stream, buf, opaque):    fd = opaque    os.write(fd, buf)THUMBNAIL_SIZE =(256, 256)thumbnail = '/home/hcc/test/screenshot/test-' + str(uuid.uuid4())command = "touch " + thumbnailprint(command)os.system(command)fd = os.open(thumbnail, os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0644)try:    conn = libvirt.open('qemu:///system')    d1 = conn.lookupByName('test')    print(d1.info())    print(d1.name())    stream = conn.newStream(0)    d1.screenshot(stream, 0, 0)    stream.recvAll(handler, fd)    if os.path.getsize(thumbnail) == 0:        image = Image.new("RGB", THUMBNAIL_SIZE, 'black')        image.save(thumbnail)    else:        print("else")        im = Image.open(thumbnail)        im.thumbnail(THUMBNAIL_SIZE)        im.save(thumbnail,'PNG')except libvirt.libvirtError:    try:        stream.abor()    except:        passelse:    stream.finish()finally:    os.close(fd)

评论关闭