由于自己管理的云服务器数量比较多,时不时需要更换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. maven 打包时动态替换properties资源文件中的配置值

    pom build节点下面添加resource配置: <resources> <resource> <directory>src/main/resources/&l ...

  2. 【题解】洛谷P3435 [POI2006] OKR-Periods of Words(KMP)

    洛谷P3435:https://www.luogu.org/problemnew/show/P3435 思路 来自Kamijoulndex大佬的解释 先把题面转成人话: 对于给定串的每个前缀i,求最长 ...

  3. webservice 从客户端中检测到有潜在危险的 Request.Form 值

    webservice中传递xml格式的参数时报错 webservice 从客户端中检测到有潜在危险的 Request.Form 值解决方案: 1.在web.config的system.web节点中添加 ...

  4. GoBelieve IM 服务端编译

    #部署im1. 安装go编译环境参考链接:https://golang.org/doc/install 2. 下载im_service代码 cd $GOPATH/src/github.com/GoBe ...

  5. JAVA给你讲它的故事

    计算机语言如果你将它当做一个产品,就像我们平时用的电视机.剃须刀.电脑.手机等, 他的发展也是有规律的. 任何一个产品的发展规律都是:向着人更加容易使用.功能越来越强大的方向发展. 那么,我们的计算机 ...

  6. binlog2sql 用法

    binlog2sql 用法 使用场景:binlog2sql是根据mysql的binlog (要求格式是row)反解析出delete,update操作,对误操作数据进行还原. https://githu ...

  7. 在tornado中使用异步mysql操作

    在使用tornado框架进行开发的过程中,发现tornado的mysql数据库操作并不是一步的,造成了所有用户行为的堵塞.tornado本身是一个异步的框架,要求所有的操作都应该是异步的,但是数据库这 ...

  8. 迪米特法则(LoD)

    如果两个类不必彼此直接通信,那么这两个类就不应当发生直接的相互作用.如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用.其根本思想是类之间的松耦合. 类之间的耦合越弱,越有利于 ...

  9. 从oracle往greenplum迁移,查询性能不满足要求的定位以及调优过程

    一.前言 在一次对比oracle和greenplum查询性能过程中,由于greenplum查询性能不理想,因此进行定位分析,提升greenplum的查询性能 二.环境信息 初始情况下,搭建一个小的集群 ...

  10. 关于mysql 8.0.13zip包安装

    mysql 8.0.13默认有一个data文件夹,这个文件夹得删了,不然安装服务时候会有日志文件提示报错: Failed to find valid data directory. Data Dict ...