如何关闭或无效的连接由vSphere-automation-sdk-python创建

发布于 2025-02-11 05:15:57 字数 641 浏览 0 评论 0原文

我正在关注https://github.com/vmware/vsphere-automation-sdk-python/tree/master/master/samples/vsphere/vcenter/vcenter/vm创建我的python库来做我的python图书馆VMS的操作。示例提供了获得新连接的代码。我在这里粘贴样本。

# Connect to vAPI Endpoint on vCenter system
session = get_unverified_session() if args.skipverification else None
client = create_vsphere_client(server=server,
                               username=username,
                               password=password],
                               session=session)

我缺少的是,一旦我在VM上完成操作,我应该如何关闭连接或无效连接。我可以简单地说为client = none即可无效连接吗?还是有记录的方法可以做到这一点?

谢谢, 马哈夫

I am following the samples provided at https://github.com/vmware/vsphere-automation-sdk-python/tree/master/samples/vsphere/vcenter/vm to create my python library for doing operations on the VMs. The examples provide the code to get a new connection. I am pasting the sample here.

# Connect to vAPI Endpoint on vCenter system
session = get_unverified_session() if args.skipverification else None
client = create_vsphere_client(server=server,
                               username=username,
                               password=password],
                               session=session)

What I am missing is once I finish my operation on the VM, how should I close the connection or invalidate the connection. Can I simply say as client = None to invalidate the connection ? or is there a documented way to do this ?

Thanks,
Madhav

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森罗 2025-02-18 05:15:57

希望这会有所帮助。

呼叫“ create_vsphere_client”返回的对象具有2种方法: exit ()和 del ()。调用这些方法中的任何一种以删除会话。就您而言,只需运行客户端即可。 del ()最后。

def __exit__(self, exc_type, exc_val, exc_tb):
   self.__del__()


def __del__(self):
    try:
        self.session_svc.delete()
    except Exception:
        # Catch exceptions when terminating the vSphere session and go ahead
        # close the requests session.
        pass
    if hasattr(self, 'session'):
        self.session.close()

Dibleiamer:我来自Java World,因此上面的术语受到此影响。

Hope this helps.

The object returned by the call 'create_vsphere_client' has 2 methods: exit() and del(). Invoke any of these methods to delete the session. In your case, simply run client.del() at the end.

def __exit__(self, exc_type, exc_val, exc_tb):
   self.__del__()


def __del__(self):
    try:
        self.session_svc.delete()
    except Exception:
        # Catch exceptions when terminating the vSphere session and go ahead
        # close the requests session.
        pass
    if hasattr(self, 'session'):
        self.session.close()

Discliamer: I come from Java world, so the terminology above is influenced by this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文