由于自己管理的云服务器数量比较多,时不时需要更换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)

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

腾讯云API弹性公网IP踩坑的更多相关文章

  1. 腾讯云无法绑定公网IP问题解释与解决方案。

    http://blog.csdn.net/chenggong2dm/article/details/51475222 解释:公网IP并不直接配置在服务器上,而是在服务器外部的路由上,通过某种映射连接. ...

  2. 阿里云Open API自动化脚本—ECS公网IP转化弹性公网IP

    1.OpenAPI Explorer 记录一下使用阿里云 Open API 自动化/脚本化 “ECS 公网 IP 转化弹性公网 IP”的实现 全过程.原博客地址:https://www.markedi ...

  3. 阿里云专有网络与弹性公网IP

    阿里云服务器经典网络和专有网络究竟有什么区别? 在用户提交订单购买阿里云ECS云服务器时,会面临怎样选择网络类型的烦恼,阿里云服务器定制购买时,网络类型里的经典网络和专有网络(VPC)是什么含义,该怎 ...

  4. 【小技巧解决大问题】使用 frp 突破阿里云主机无弹性公网 IP 不能用作 Web 服务器的限制

    背景 今年 8 月份左右,打折价买了一个阿里云主机,比平常便宜了 2000 多块.买了之后,本想作为一个博客网站的,毕竟国内的服务器访问肯定快一些.满心欢喜的下单之后,却发现 http 服务,外网怎么 ...

  5. 阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性公网IP是独立的公网IP资源,可以绑定到阿里云专有网络VPC类型的ECS、NAT网关、私网负载均衡SLB上,并可以动态解绑,实现公网IP和ECS、NAT网关、SLB的解耦,满足灵活管理的要求。阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性络VPC类型的E

    阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性公网IP是独立的公网关.私网负载均衡SLB上,并可以动态解绑,实现公网IP和ECS.NAT网关.SLB的解耦,满足灵活管理的要求.

  6. 什么是弹性公网IP?

    弹性公网IP(Elastic IP Address,简称EIP),是可以独立购买和持有的公网IP地址资源.目前,EIP可绑定到专有网络类型的ECS实例.专有网络类型的私网SLB实例.专有网络类型的辅助 ...

  7. 浅析弹性公网IP付费模式和短时升配功能介绍

    ​ 弹性公网IP付费模式对比 弹性公网IP(EIP),有两种付费方式.一种是预付费,一种是后付费.对于预付费弹性公网IP而言,最大的优点就是带宽费用便宜,相对于后付费有比较大的优惠. 例如,杭州地域6 ...

  8. 腾讯云主机的公网无法访问,putty和FileZilla连接不上

    1.解决方法一(之前百度都是这种安全组忘了添加) 2.解决方案二(ps:我是用centos的,然后不知道为什么访问不了,端口也是全部开的) service network restart 重置网络命令 ...

  9. .NET调用腾讯云API实例

    最近项目有用到腾讯云的身份识别接口,话不多说,直接上代码: private void IDCardVerification(HttpContext context) { string imgStr = ...

随机推荐

  1. 使用nuget 打包并上传 nuget.org

    一. 准备工作 1 下载  Download NuGet.exe 2  windows 系统下设置环境变量 path中 或者 在dos 命令窗口下转到 nuget.exe 所在目录 3 在www.nu ...

  2. 架构风格:万金油CS与分层

    计算机科学家David Wheele曾说过这么一句话: All problems in computer science can be solved by another level of indir ...

  3. 轻量ORM-SqlRepoEx (八)MySQL、Sql Service 迁移

    数据库变更在编程应用中是常的,MySQL.Sql Service之间的数据迁移更为常见,在 SqlRepoEx2.0DemoForAspCore中演示了,这种数据库之间切换时SqlRepoEx是如何的 ...

  4. 一点一点看JDK源码(五)java.util.ArrayList 后篇之SubList

    一点一点看JDK源码(五)java.util.ArrayList 后篇之SubList liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点看JDK源码(〇) S ...

  5. js的垃圾回收机制

    Js具有自动垃圾回收机制.垃圾收集器会按照固定的时间间隔周期性的执行. JS中最常见的垃圾回收方式是标记清除. 工作原理:是当变量进入环境时,将这个变量标记为“进入环境”.当变量离开环境时,则将其标记 ...

  6. 737 MAX 8-Think

    波音 737 MAX 8的事故,凸显传感器数据在失真的情况下,软件系统需要如何设计的问题:这点感觉波音那么大的公司,不应该不会没有考虑到. 正常来说传感器给出错误的数据,通常是计算出错误的结果,就像做 ...

  7. linux ping命令实践

          ping 解析       Linux系统的ping命令是常用的网络命令,它通常用来检测与目标主机的连通性,经常说"ping以下机器,看是否开着,不能打开网页时候,可以ping ...

  8. jQuery获取Select option 选择的Text和 Value

    获取一组radio被选中项的值:var item = $('input[name=items][checked]').val();获取select被选中项的文本var item = $("s ...

  9. node.js使用Sequelize 操作mysql

    Sequelize就是Node上的ORM框架 ,相当于java端的Hibernate 是一个基于 promise 的 Node.js ORM, 目前支持 Postgres, MySQL, SQLite ...

  10. linux 操作之压缩与解压

    压缩与解压 压缩格式:zip , rar , 7z , iso , exe gzip (以下是命令操作) gzip [文件名]  #压缩成gz格式 , 原来的文件消失 , 不支持目录. bzip2 - ...