腾讯云API弹性公网IP踩坑

由于自己管理的云服务器数量比较多,时不时需要更换IP,在管理台上一下下点击,实在浪费时间,于是就想到了通过API调用的方式,将更换IP一系列动作,全部集成到Python代码里面,实现一行命令,完成IP更换,由于人懒,就先

把最核心的代码(虽然都是腾讯云生成的)、流程、坑点贴出来,仅供菜鸟参考,高手无视!已先在腾讯云社区编辑发布,请管理员勿认为是转载!

具体步骤:

一 进入 https://cloud.tencent.com/document/api ,

页面左侧列表查找“私有网络”---“弹性公网相关接口”,就可以看到对应接口的文档了

二 选择一个接口,然后点击“API 3.0 Exploper”,进入到开发者工具页面,

坑点一:下面代码,对于菜鸟来说,引入的相关模块直接用“pip install 对应模块名”会报错

看第一张图sdk,相关语言sdk,点击就可以看到github上各语言的库引用方式

三 直接copy开发者工具上的代码,当然你也可以看下图代码

坑点二:"SecretId ","SecretKey" 这两个值是你调用API,腾讯用来确认你身份的凭证,

在哪里申请呢?腾讯云的首页,“云产品”--“管理工具”--“云API秘钥”,

没有使用过的话,是不会在你的控制台上显示的。

坑点三:由于腾讯云API文档的不合理,导致生成的代码有一些坑,

具体坑信息我已在代码里通过注释的方式解释了

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.vpc.v20170312 import vpc_client, models

#查询弹性IP
def findIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.DescribeAddressesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.DescribeAddresses(req)
        #eip=resp.to_json_string()[34:61]
        eip=resp.to_json_string()[48:60]
        print(eip)   #打印结果:"AddressId": "eip-ilzg91oy"
        return eip
    except TencentCloudSDKException as err:
        print(err)

#解除绑定IP
def unbindingIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.DisassociateAddressRequest()
        eip = findIp()
        params = eip
        #req.from_json_string(params)
        req.AddressId=params   #这里修改了一下,官网生成的是上一行代码
        resp = client.DisassociateAddress(req)
        print(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)
#释放IP
def releaseIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)

        req = models.DescribeAddressesRequest()
        eip = findIp()
        list=[]
        list.append(eip)
        params = list
        req.AddressIds=params  #这里修改了一下,要求传数组
        resp = client.ReleaseAddresses(req)
        print(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

#创建IP
def newIp():
    try:
        cred = credential.Credential("SecretId", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.AllocateAddressesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.AllocateAddresses(req)
        print(resp.to_json_string())

    except TencentCloudSDKException as err:
        print(err)

#绑定弹性IP
def binding():
    try:
        cred = credential.Credential("SecretId", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)

        req = models.AssociateAddressRequest()
        #params = '{"ins-61lwor90"}'
        #req.from_json_string(params)
        #这是只传AddressId的报错:MissingAssociateEntity message:You need to specify the entity ID `"InstanceId"` or `"NetworkInterfaceId"` to associate this address. requestId:81702256-e75f-458f-afde-e87a69554f83
        #所以至少要传两个值
        req.InstanceId = "ins-61cwor70"
        eip = findIp()
        req.AddressId=eip
        resp = client.AssociateAddress(req)
        print(resp.to_json_string())

    except TencentCloudSDKException as err:
        print(err)

创建了一个微信群,目前看来是纯技术,虽然大家技术都不咋样,欢迎菜鸟或大牛的加入,目前看来还能算是个纯技术的讨论群,勿培训,勿卖视频

原文地址:https://www.cnblogs.com/wujianqinjian/p/9838455.html