---
title: "[VDI] spice-gtk 的python api 调用"
author: "ezzze"
date: "2012-10-20T14:20:04Z"
tags: []
categories: ["开源软件"]
slug: "vdi-spice-gtk-python-api"
draft: false
lastmod: "2012-10-20T14:32:26Z"
id: 1702157
url: "https://ezzze.iteye.com/blog/1702157"
---

**关于SPICE：**

spice 是 redhat 的虚拟化远程桌面协议 又叫 独立计算环境初级协议(Simple Protocol for Independent Computing Environments，就不多介绍了，反正做VDI的都知道。

现在[spice-gtk](http://spice-space.org/download.html) 到了 0.14了，功能也算是比较全了，当然大家想要的3d 加速，视频加速还是木有的。

但是作为测试后端kvm，不失为一个好的远程协议，至少比vnc强多了。

对我来说,不过spice-gtk还是稍嫌笨重。好在它有api，还是python的..........

我应该如何调用spice-gtk呢？

**我们需要做一些准备工作**

 

a. 编译spice-gtk的时候，带python支持： --with-gtk=2.0

b. 下载和安装python gtk

这些做完后，把生成的 SpiceClientGtk.so 放到你可以访问的地方，然后就开始调用吧

 

 

 

 

```
#!/usr/bin/env python
import SpiceClientGtk as _spice
import pygtk
import gtk
import gobject

class T_Window(gtk.Window):
    def __init__(self):
        super(Tcloud_Window, self).__init__()
        self.set_title(&quot;tcloud-client&quot;)
        self.set_size_request(1024, 768)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
        self.set_position(gtk.WIN_POS_CENTER)
        
        self.session = None
        self.display = None
        self.display_channel = None

    def settings(self,host,port):
        self.session = _spice.Session()
        
        uri = &quot;spice://&quot;
        uri += str(host) + &quot;?port=&quot; + str(port)
        self.session.set_property(&quot;uri&quot;,uri)
   
    def _channel_new(self, session, channel):

        if type(channel) == _spice.MainChannel:
            return

        if type(channel) == _spice.DisplayChannel:
            channel_id = channel.get_property(&quot;channel-id&quot;)
            self.display_channel = channel
            self.display = _spice.Display(self.session, channel_id)
            self.add(self.display)
            self.display.realize()
            self.display.show()
            return
        
    
        
    def main_loop(self,host,port,password=None):
        
        self.settings(host, port)
        if password:
            self.session.set_property(&quot;password&quot;, password)
            
        gobject.GObject.connect(self.session, &quot;channel-new&quot;,
                                self._channel_new)
        self.session.connect()
        
        def close(self):
            if self.session is not None:
                self.session.disconnect()
            self.session = None
            self.audio = None
            if self.display:
                self.display.destroy()
            self.display = None
            self.display_channel = None  
            gtk.main_quit()  
            
        self.connect(&quot;destroy&quot;, close)
        self.show_all()
        gtk.main();
       
    
if __name__ == '__main__':
    tw = T_Window()
    host = &quot;localhost&quot;
    port =&quot;5900&quot;
    tw.main_loop(host,port)
```

 

嘛  以上都是在linux上做的 亲 故意写最后