python 使用libvirt抓取linux kvm虚拟机的缩略图
当前位置:以往代写 > Python教程 >python 使用libvirt抓取linux kvm虚拟机的缩略图
2019-06-14

python 使用libvirt抓取linux kvm虚拟机的缩略图

python 使用libvirt抓取linux kvm虚拟机的缩略图
python">import libvirt
import os
import uuid
try:
    from PIL import Image
    print("PIL")
except ImportError:
    import Image
def 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 " + thumbnail
print(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:
        pass
else:
    stream.finish()
finally:
    os.close(fd)

标签:python

    关键字:

在线提交作业