我正在尝试从Python使用Virtualbox 6.1的SOAP接口来获取计算机的屏幕截图。我可以启动机器,但是每当尝试检索屏幕布局时都会出现锁定错误。
这是代码:
import zeep
# helper to show the session lock status
def show_lock_state(session_id):
session_state = service.ISession_getState(session_id)
print('current session state:', session_state)
# connect
client = zeep.Client('http://127.0.0.1:18083?wsdl')
service = client.create_service("{http://www.virtualbox.org/}vboxBinding", 'http://127.0.0.1:18083?wsdl')
manager_id = service.IWebsessionManager_logon('fakeuser', 'fakepassword')
session_id = service.IWebsessionManager_getSessionObject(manager_id)
# get the machine id and start it
machine_id = service.IVirtualBox_findMachine(manager_id, 'Debian')
progress_id = service.IMachine_launchVMProcess(machine_id, session_id, 'gui')
service.IProgress_waitForCompletion(progress_id, -1)
print('Machine has been started!')
show_lock_state(session_id)
# unlock and then lock to be sure, doesn't have any effect apparently
service.ISession_unlockMachine(session_id)
service.IMachine_lockMachine(machine_id, session_id, 'Shared')
show_lock_state(session_id)
console_id = service.ISession_getConsole(session_id)
display_id = service.IConsole_getDisplay(console_id)
print(service.IDisplay_getGuestScreenLayout(display_id))
机器正常启动,但最后一行给出了错误VirtualBox error: rc=0x80004001
,从我的阅读中可以看出这表示会话已锁定。
我试图再次释放并获得该锁,但是即使成功,该错误仍然存在。我浏览了文档,但是找不到我应该使用的其他类型的锁,除了Write锁之外,因为计算机正在运行,Write锁在这里不可用。我找不到任何语言的示例。
[我发现了一个名为VBoxManager的Android应用,它具有此SOAP屏幕快照功能。通过MITM代理运行它,我重构了它执行的调用并将它们写为与Zeep等效的调用。如果有人对未来感兴趣,现在上面脚本的最后几行是:
console_id = service.ISession_getConsole(session_id)
display_id = service.IConsole_getDisplay(console_id)
resolution = service.IDisplay_getScreenResolution(display_id, 0)
print(f'display data: {resolution}')
image_data = service.IDisplay_takeScreenShotToArray(
display_id,
0,
resolution['width'],
resolution['height'],
'PNG')
with open('screenshot.png', 'wb') as f:
f.write(base64.b64decode(image_data))