腾讯云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)
创建了一个微信群,目前看来是纯技术,虽然大家技术都不咋样,欢迎菜鸟或大牛的加入,目前看来还能算是个纯技术的讨论群,勿培训,勿卖视频

腾讯云API弹性公网IP踩坑的更多相关文章
- 腾讯云无法绑定公网IP问题解释与解决方案。
http://blog.csdn.net/chenggong2dm/article/details/51475222 解释:公网IP并不直接配置在服务器上,而是在服务器外部的路由上,通过某种映射连接. ...
- 阿里云Open API自动化脚本—ECS公网IP转化弹性公网IP
1.OpenAPI Explorer 记录一下使用阿里云 Open API 自动化/脚本化 “ECS 公网 IP 转化弹性公网 IP”的实现 全过程.原博客地址:https://www.markedi ...
- 阿里云专有网络与弹性公网IP
阿里云服务器经典网络和专有网络究竟有什么区别? 在用户提交订单购买阿里云ECS云服务器时,会面临怎样选择网络类型的烦恼,阿里云服务器定制购买时,网络类型里的经典网络和专有网络(VPC)是什么含义,该怎 ...
- 【小技巧解决大问题】使用 frp 突破阿里云主机无弹性公网 IP 不能用作 Web 服务器的限制
背景 今年 8 月份左右,打折价买了一个阿里云主机,比平常便宜了 2000 多块.买了之后,本想作为一个博客网站的,毕竟国内的服务器访问肯定快一些.满心欢喜的下单之后,却发现 http 服务,外网怎么 ...
- 阿里云弹性公网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的解耦,满足灵活管理的要求.
- 什么是弹性公网IP?
弹性公网IP(Elastic IP Address,简称EIP),是可以独立购买和持有的公网IP地址资源.目前,EIP可绑定到专有网络类型的ECS实例.专有网络类型的私网SLB实例.专有网络类型的辅助 ...
- 浅析弹性公网IP付费模式和短时升配功能介绍
弹性公网IP付费模式对比 弹性公网IP(EIP),有两种付费方式.一种是预付费,一种是后付费.对于预付费弹性公网IP而言,最大的优点就是带宽费用便宜,相对于后付费有比较大的优惠. 例如,杭州地域6 ...
- 腾讯云主机的公网无法访问,putty和FileZilla连接不上
1.解决方法一(之前百度都是这种安全组忘了添加) 2.解决方案二(ps:我是用centos的,然后不知道为什么访问不了,端口也是全部开的) service network restart 重置网络命令 ...
- .NET调用腾讯云API实例
最近项目有用到腾讯云的身份识别接口,话不多说,直接上代码: private void IDCardVerification(HttpContext context) { string imgStr = ...
随机推荐
- 移动端web开发技巧和常见问题
常见问题 1.移动端如何定义字体font-family 三大手机系统的字体: ios 系统 默认中文字体是Heiti SC 默认英文字体是Helvetica 默认数字字体是HelveticaNeue ...
- 【AngularJS学习笔记】Java Script "use strict" 严格模式
---恢复内容开始--- 学习Angular的时候,发现好多优秀的源码中,JS文件的最上面,都会写上"use strict"; 这几个字符,搜了一下,找到一篇比较不错的文章,抄过来 ...
- EF Core中关于System.Linq.Dynamic.Core的使用(转载)
项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic.Core用起来比 ...
- 全渠道价值链整合云服务 B2B SOLOMO(组图)
最近看到>中谈到“全渠道零售”(Omni-channel Retailing),指在互联网和电子商务的当今时代“零售商将能通过各种渠道与顾客互动,包括网站.实体店.服务终端.直邮和目录.呼叫中心 ...
- iOS:UICollectionView流式布局及其在该布局上的扩展的线式布局
UICollectionViewFlowLayout是苹果公司做好的一种单元格布局方式,它约束item的排列规则是:从左到右依次排列,如果右边不够放下,就换一行重复上面的方式排放,,,,, 常用的 ...
- 通过非聚集索引让select count(*) from 的查询速度提高几十倍、甚至千倍
通过非聚集索引,可以显著提升count(*)查询的性能. 有的人可能会说,这个count(*)能用上索引吗,这个count(*)应该是通过表扫描来一个一个的统计,索引有用吗? 不错,一般的查询,如果用 ...
- parsing XML document from class path resource [applicationtext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationtext.xml] cannot be opened because it does not e
控制台异常: parsing XML document from class path resource [applicationtext.xml]; nested exception is java ...
- RHEL6(RedHat6)和SUSE11系统配置IPV6地址
临时生效 RHEL6和SUSE11系统临时配置IPv6地址操作是一样的,比如添加如下ipv6地址. ip - addr add ::A/ dev eth2 ip - route add default ...
- 第一课、安装登录CentOS7
一.学习之初 1.学习这个课程的目的是,高薪就业,搞运维. 2.应该在宁波发展. 3.大概给自己定的计划是4个月能学习2遍. 4.学好之后就跳槽. 5.2年左右的时间要达到1.5W争取. 学习方法: ...
- 关于vue中mockjs的使用
使用vue的时候,后台可能不能及时作出接口,那么就需要我们前端自己模拟数据,使用mockjs可以进行模拟数据. 首先安装mockjs,cnpm install mockjs --save-dev: 其 ...