由于自己管理的云服务器数量比较多,时不时需要更换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. OC对象里面包含的结构体类型的属性,不能对该结构体属性的成员单个进行修改

    OC对象里面包含的结构体类型的属性,不能对该结构体属性的成员单个进行修改,需要对OC对象的结构体属性整体赋值. 关于网上很多博客写着“结构体类型里面的某个属性如果想要修改是不允许单个修改的” 之解释: ...

  2. Winodws SNMP服务安装和配置(Windows 2003 & 2008 R2)

    简单网络管理协议SNMP服务起着代理的作用,它会收集可以向SNMP管理站或控制台报告的信息.您可以使用SNMP服务来收集数据,并且在整个公司网络范围内管理基于Windows Server 2003.M ...

  3. Before start of result set

    ResultSet:在处理结果集的时候出现了问题. 解决办法:while(rs.next())

  4. NLP语言模型

    语言模型: I. 基本思想 区别于其他大多数检索模型从查询到文档(即给定用户查询,如何找出相关的文档), 语言模型由文档到查询,即为每个文档建立不同的语言模型,判断由文档生成用户查 询的可能性有多大, ...

  5. 听说玩JAVA,必须过JDK这关?

    JDK是什么?JRE是什么?JDK和JRE的区别? Java Runtime Environment (JRE) 包含: Java虚拟机.库函数.运行Java应用程序和Applet所必须文件 Java ...

  6. plsql中特殊字符的处理

    --去除换行chr(10) update zhzl_address t set t.add_administration_num=replace(t.add_administration_num,ch ...

  7. React--- react 初见React 总结

    简介 react 程序代码是透明的,需要什么装什么 代码实现逻辑清晰可见 第一天 React  基础构造 分别是  继承的 React.component(继承的依赖类)/dom(dom元素)/pro ...

  8. 红帽RHEL6.8离线环境下升级到RHEL7.3

    Red Hat Enterprise Linux 7 (RHEL 7) 是第一个支持从前一个 RHEL 主发行版本(RHEL 6)进行原位(in-place)升级的 RHEL 主版本.原位升级(in- ...

  9. Delphi的TValue探索(一)

    TValue是Delphi的RTTI系统的重要类型. 经过摸索,发现TValue功能强大,可以实现很多功能.本文章中所有程序采用XE3运行通过. 一.TValue结构 TValue定义在System. ...

  10. 大数据学习--day04(选择结构、循环结构、大数据java基础面试题)

    选择结构.循环结构.大数据java基础面试题 switch: 注意: byte short int char String(jdk1.7支持) 不能是 long float double boolea ...