天天看點

[VDI] spice-gtk 的python api 調用

關于SPICE:

spice 是 redhat 的虛拟化遠端桌面協定 又叫 獨立計算環境初級協定(Simple Protocol for Independent Computing Environments,就不多介紹了,反正做VDI的都知道。

現在spice-gtk 到了 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("tcloud-client")
        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 = "spice://"
        uri += str(host) + "?port=" + str(port)
        self.session.set_property("uri",uri)
   
    def _channel_new(self, session, channel):

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

        if type(channel) == _spice.DisplayChannel:
            channel_id = channel.get_property("channel-id")
            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("password", password)
            
        gobject.GObject.connect(self.session, "channel-new",
                                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("destroy", close)
        self.show_all()
        gtk.main();
       
    
if __name__ == '__main__':
    tw = T_Window()
    host = "localhost"
    port ="5900"
    tw.main_loop(host,port)
           

嘛  以上都是在linux上做的 親 故意寫最後