#需要导入腾讯云API的库
#pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python
#如果用的pycharm则需在控制台导入,参考底部
import json
import tabulate
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models
from tabulate import tabulate
#需要登陆腾讯云控制台获取SecretId, SecretKey相关值填入
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "cvm.ap-guangzhou.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = cvm_client.CvmClient(cred, "ap-guangzhou", clientProfile)
def funct1():
# 实例化一个请求对象,每个接口都会对应一个request对象
count = int(input("请输入你要购买的虚拟机数量:"))
name01 = str(input(("请输入虚拟机主机名称")))
ip = [str(input("请输入IP地址:"))]
req = models.RunInstancesRequest()
params01 = {
"InstanceChargeType": "POSTPAID_BY_HOUR",
"DisableApiTermination": False,
"Placement": {
"Zone": "ap-guangzhou-3",
"ProjectId": 0
},
"VirtualPrivateCloud": {
"AsVpcGateway": False,
"VpcId": "vpc-gr8nkzaj",
"SubnetId": "subnet-rcof46b8",
"Ipv6AddressCount": 0,
"PrivateIpAddresses": ip
},
"InstanceType": "S5.MEDIUM2",
"ImageId": "img-9id7emv7",
"SystemDisk": {
"DiskSize": 50,
"DiskType": "CLOUD_BSSD"
},
"InternetAccessible": {
"InternetMaxBandwidthOut": 0,
"PublicIpAssigned": False
},
"InstanceName": name01,
"LoginSettings": {
"Password": "P@ssw0rd123.com"
},
"SecurityGroupIds": [ "sg-b8dxpi79" ],
"InstanceCount": count,
"EnhancedService": {
"SecurityService": {
"Enabled": True
},
"MonitorService": {
"Enabled": True
},
"AutomationService": {
"Enabled": True
}
},
"HostName": name01
}
req.from_json_string(json.dumps(params01))
# 返回的resp是一个RunInstancesResponse的实例,与请求对象对应
resp = client.RunInstances(req)
# 输出json格式的字符串回包
print(resp.to_json_string)
print("已经成功完成如上服务器创建")
def funct2():
req = models.TerminateInstancesRequest()
params = {
"InstanceIds": del01
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个TerminateInstancesResponse的实例,与请求对象对应
resp = client.TerminateInstances(req)
# 输出json格式的字符串回包
print(resp.to_json_string())
def funct3():
req = models.DescribeInstancesRequest()
params = {
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个DescribeInstancesResponse的实例,与请求对象对应
resp = client.DescribeInstances(req)
# 输出json格式的字符串回包
json_str = resp.to_json_string()
data = json.loads(json_str)
instance_list = data["InstanceSet"]
print(tabulate(instance_list, headers="keys"))
while True:
print('''你正在运行腾讯云脚本
1 创建虚拟机实例
2 删除实例虚拟机
3 查询实例列表
4 退出''')
try:
num = int(input("请输入对应序号执行程序:"))
except ValueError:
print("输入有误,请重新输入")
continue
if num == 1:
funct1()
elif num == 2:
del01 = [str(input("请输入要删除的机器ID:"))]
funct2()
elif num == 3:
funct3()
elif num == 4:
exit()
else:
print("输入有误,请重新输入")
pycharm导入腾讯云Python的API模块,
运行效果如下: