python 操作 azure 虚拟机

下载微软的 python SDK

pip install azure

但是默认的 azure 接口对国内的 azure 无效,修改Python27\Lib\site-packages\azure\__init__.py文件指向China Azure

源文件如下:

# Live ServiceClient URLs
BLOB_SERVICE_HOST_BASE = '.blob.core.windows.net'
QUEUE_SERVICE_HOST_BASE = '.queue.core.windows.net'
TABLE_SERVICE_HOST_BASE = '.table.core.windows.net'
SERVICE_BUS_HOST_BASE = '.servicebus.windows.net'
MANAGEMENT_HOST = 'management.core.windows.net'

修改为:

# Live ServiceClient URLs
BLOB_SERVICE_HOST_BASE = '.blob.core.chinacloudapi.cn'
QUEUE_SERVICE_HOST_BASE = '.queue.core.chinacloudapi.cn'
TABLE_SERVICE_HOST_BASE = '.table.core.chinacloudapi.cn'
SERVICE_BUS_HOST_BASE = '.servicebus.chinacloudapi.cn'
MANAGEMENT_HOST = 'management.core.chinacloudapi.cn'

启动和停止虚拟机

from azure.servicemanagement import *

subscription_id = 'xxxxxxxx'
# 订阅ID
certificate_path = 'xxxxxxxx'
# pem 证书路径

sms = ServiceManagementService(subscription_id, certificate_path)


sms.shutdown_role('orange', 'oranged', 'oranged', post_shutdown_action='Stopped')
sms.start_role('xxx', 'xxx', 'xxx')

LinuxMac命令行生成 azure 证书

openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
openssl x509 -inform pem -in mycert.pem -outform der -out mycert.cer

上传cer证书到控制台证书管理里面后,就可以在代码中操作azure了。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐