2015年8月21日 星期五

使用 pyVmomi 時略過 HTTPS 警告

在使用 pyVmomi 連接 VMware 的 API 時,第一個遇到的問題是程式出現以下的警告:
/usr/lib/python2.6/site-packages/requests-2.7.0-py2.6.egg/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
看起來就是因為連線要使用 HTTPS,但是沒有給予憑證的問題~。
不過簡單地搜尋一下,就看到 [1] 的解法,只要把 requests 這個套件的警告關掉就可以了。
import atexit
import requests
from pyVim import connect

# Disable the checking of certification.
requests.packages.urllib3.disable_warnings()
# Initiate the connection to VMware vSphere.
service_instance = connect.SmartConnect(
        host="192.168.200.200",
        user="administrator@vsphere.local",
        pwd="qwer",
        port=443)
atexit.register(connect.Disconnect, service_instance)

# Get the session ID from the connection.
session_id = service_instance.content.sessionManager.currentSession.key
print("Session ID: %s" % session_id)

程式印出的回覆如下:
Session ID: 521eab6e-7ef1-d7e6-1f67-ba38dd7e2487

參考資料:
  1. Surpress InsecureRequestWarning: Unverified HTTPS request is being made in Python2.6

沒有留言: