Python VirtualBox API

3 投票
1 回答
10231 浏览
提问于 2025-04-15 19:30

我做了一个命令行界面,可以让你从远程电脑控制VirtualBox。现在我想用Python的VirtualBox API来实现这个命令行界面。为此,我下载了pyvb这个包(Python的API文档里有可以用来实现这个功能的函数)。但是,当我输入 pyvb.vb.VB.startVM(instance of VB class,pyvb.vm.vbVM)

服务器端的代码是

from pyvb.constants import *

from pyvb.vm import *

from pyvb.vb import *

import xpcom

import pyvb

import os

import socket

import threading

class ClientThread ( threading.Thread ):

   # Override Thread's __init__ method to accept the parameters needed:
   def __init__ ( self, channel, details ):

      self.channel = channel
      self.details = details
      threading.Thread.__init__ ( self )

   def run ( self ):

      print 'Received connection:', self.details [ 0 ]
      while 1:     
         s= self.channel.recv ( 1024 )
         if(s!='end'):
            if(s=='start'):
              v=VB() 
              pyvb.vb.VB.startVM(v,pyvb.vm.vbVM)

         else:
           self.channel.close()
           break
      print 'Closed connection:', self.details [ 0 ]


server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '127.0.0.1', 2897 ) )
server.listen ( 5 )

while True:
   channel, details = server.accept()
   ClientThread ( channel, details ).start()

结果却出现了一个错误

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner
    self.run()
  File "news.py", line 27, in run
    pyvb.vb.VB.startVM(v,pyvb.vm.vbVM.getUUID(m))
  File "/usr/lib/python2.5/site-packages/pyvb-0.0.2-py2.5.egg/pyvb/vb.py", line 65, in startVM
    cmd='%s %s'%(VB_COMMAND_STARTVM, vm.getUUID())
AttributeError: 'str' object has no attribute 'getUUID'

1 个回答

3

你可以去看看Virtualbox的官方Python接口文档。pyvb看起来是一个第三方写的封装工具。

Virtualbox的开发工具包里有Python的示例代码和完整的接口文档。

撰写回答