Python批量绑定腾讯云平台CVM域名与端口

简介:

腾讯云平台绑定域名端口是要一个个绑,比较麻烦,之前看了一下API,简单写了一个绑域名端口的脚本:


ApiLogger.py(这个是输出日志用的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
#coding:utf-8
import  logging
#日志模块
logger  =  logging.getLogger( "sy_tools" )
logger.setLevel(logging.DEBUG)
ch  =  logging.StreamHandler()
ch.setLevel(logging.DEBUG)
fh  =  logging.FileHandler( "sy_tools.log" )
fh.setLevel(logging.DEBUG)
formatter  =  logging.Formatter( "%(asctime)s - %(levelname)s - %(message)s" )
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)


ApiRequest.py (这个API提交信息的基础方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
#coding:utf-8
import  urllib2
import  base64
import  json
import  time
import  random
import  hmac
import  hashlib
import  ApiLogger
logger  =  ApiLogger.logger
class  TX_API:
     def  __init__( self ):
         self .accessId  =  'accessid'
         self .secretKey  =  'secretKey'
         self .endPoint  =  'http://api.yun.qq.com'
         self .uri  =  ''
         self .body  =  ''
         self .method  =  ''   
                                                                                                                      
     def  set_uri( self , value):
         self .uri  =  value
         return  self .uri
                                                                                                                      
     def  set_body( self , value):
         self .body  =  value
         return  self .body
                                                                                                                      
     def  set_method( self , value):
         self .method  =  value
         return  self .method
                                                                                                                      
     def  do_request( self ):
         if  self .body:
             data  =  json.dumps( self .body)
         else :
             data  =  self .body
         self .nonce  =  random.randint( 1 , 2 * * 31 - 1 )
         self .timestamp = int (time.time())
         self .orignal  =  '''body=%s&method=%s&uri=%s&x-txc-cloud-secretid=%s&x-txc-cloud-nonce=%s&x-txc-cloud-timestamp=%s'''  % (data, self .method, self .uri, self .accessId, self .nonce, self .timestamp)
         self .signature  =  base64.b64encode(hmac.new( self .secretKey, self .orignal,digestmod = hashlib.sha1).digest())
         self .header  =  {
             "Content-type" : "application/json; charset=utf-8" ,
             "x-txc-cloud-secretid" : self .accessId,
             "x-txc-cloud-nonce" : self .nonce,
             "x-txc-cloud-timestamp" : self .timestamp,
             "x-txc-cloud-signature" : self .signature,
         }
                                                                                                                          
         if  self .body:
             self .request  =  urllib2.Request( self .endPoint  +  self .uri, json.dumps( self .body))
         else :
             self .request  =  urllib2.Request( self .endPoint  +  self .uri)
         for  key  in  self .header:
             self .request.add_header(key,  self .header[key])
                                                                                                                          
         try :
             result  =  urllib2.urlopen( self .request)
         except  urllib2.HTTPError as http_error:
             print  http_error
         else :
             response  =  json.loads(result.read())
             result.close()
             return  responsep jfd


这里是做一些接口认证,比较麻烦,详细要求看下API文档

self.accessId 和 self.secretKey  这两个KEY是要自己在云平台生成的


ApiDomain.py (调用绑定域名API及返回相关状态)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python
#coding:utf-8
import  ApiRequest
import  ApiLogger
logger  =  ApiLogger.logger
def  get_domain_id(domain):
     sy  =  ApiRequest.TX_API()
     sy.set_method( 'GET' )
     sy.set_uri( '/v1/domains/query_instance_id?domains=%s'  %  domain)
     result  =  sy.do_request()
     if  result:
         return  result[ 'instanceIds' ][domain]
         #    return result['instances'][0]['instanceId']
def  bind_domain_port(domain_id, cvm_ip, cvm_port):
     sy  =  ApiRequest.TX_API()
     sy.set_method( 'POST' )
     sy.set_uri( '/v1/domains/%s/cvm_bind'  %  domain_id)
     body  =  { 'lanIps' :[cvm_ip], 'port' :cvm_port}
     sy.set_body(body)
     result  =  sy.do_request()
     if  result:
         errorCode  =  result[ 'errorCode' ]
         httpCode  =  result[ 'httpCode' ]
         logger.debug( "errorCode = %s and httpCode = %s"  %  (errorCode, httpCode))
     else :
         logger.error( "Request Error! Please try again!" )
                                                                              
def  check_domain_info(domain_id):
     sy  =  ApiRequest.TX_API()
     sy.set_method( 'GET' )
     sy.set_uri( '/v1/domains/%s'  %  domain_id)
     result  =  sy.do_request()
     if  result:
         logger.debug( "Domain '%s' already bind:"  %  result[ 'instanceInfo' ][ 'domain' ])
         for  in  result[ 'instanceInfo' ][ 'devicesList' ]:
             logger.debug( "host : %s,\tport: %s"  %  (i[ 'lanIp' ], i[ 'port' ]))
     else :
         logger.error( "Request Error! Please try again!" )
                                                                              
                                                                          
                                                                              
if  __name__  = =  "__main__" :
     print  get_domain_id( 's233.app100670828.qqopenapp.com' )
     #bind_domain_port('100000041073', "10.204.153.56", 8008)
     #check_domain_info(100000041073)


get_domain_id  : 根据域名获取它的ID

bind_domain_port: 把CVM绑定到该域名的某个端口

check_domain_info: 获取该域名绑定了哪些CVM及端口


sy_tools.py (实际工作,调用上面的接口)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/python
#coding:utf-8
import  ApiDomain
import  time
import  ApiLogger
logger  =  ApiLogger.logger
domains  =  [ 's233.app100670828.qqopenapp.com' ]
hosts  =  [ '10.207.252.117' ]
ports  =  [ '80' , '8000' '8001' '8002' '8003' '8004' '8005' ]
logger.debug( "This scripts will bind domain ports!" )
raw_input ( "Please Enter any key to start!" )
num  =  len (domains)
for  in  range ( 0 ,num):
     domain  =  domains[i]
     host  =  hosts[i]
     domain_id  =  ApiDomain.get_domain_id(domain)
     print  domain, domain_id, host
     for  port  in  ports:
         logger.debug( "bind %s port ING~~~"  %  port)
         ApiDomain.bind_domain_port(domain_id, host,  int (port))
         time.sleep( 20 )
     ApiDomain.check_domain_info(domain_id)
     time.sleep( 20 )
logger.debug( "Done!" )
raw_input ( "Please Enter any key to exit!" )


domains = ['s233.app100670828.qqopenapp.com']


hosts = ['10.207.252.117']


这里应该用字典比较好,当时写的时候就不知道怎么想的了。。后来就没改了。。

写得比较弱,不过能用就好了。。

本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1285389如需转载请自行联系原作者


lihuipeng

目录
打赏
0
0
0
0
90
分享
相关文章
Python/Anaconda双方案加持!Jupyter Notebook全平台下载教程来袭
Jupyter Notebook 是一款交互式编程与数据科学分析工具,支持40多种编程语言,广泛应用于机器学习、数据清洗和学术研究。其核心优势包括实时执行代码片段、支持Markdown文档与LaTeX公式混排,并可导出HTML/PDF/幻灯片等格式。本文详细介绍了Jupyter Notebook的软件定位、特性、安装方案(Anaconda集成环境与原生Python+PIP安装)、首次运行配置及常见问题解决方案,帮助用户快速上手并高效使用该工具。
1688平台API接口实战:Python实现店铺全量商品数据抓取
本文介绍如何使用Python通过1688开放平台的API接口自动化抓取店铺所有商品数据。首先,开发者需在1688开放平台完成注册并获取App Key和App Secret,申请“商品信息查询”权限。接着,利用`alibaba.trade.product.search4trade`接口,构建请求参数、生成MD5签名,并通过分页机制获取全量商品数据。文中详细解析了响应结构、存储优化及常见问题处理方法,还提供了竞品监控、库存预警等应用场景示例和完整代码。
Python实现智能家居设备的统一控制平台
【10月更文挑战第6天】 Python实现智能家居设备的统一控制平台
371 11
【新闻文本分类识别系统】Python+卷积神经网络算法+人工智能+深度学习+计算机毕设项目+Django网页界面平台
文本分类识别系统。本系统使用Python作为主要开发语言,首先收集了10种中文文本数据集("体育类", "财经类", "房产类", "家居类", "教育类", "科技类", "时尚类", "时政类", "游戏类", "娱乐类"),然后基于TensorFlow搭建CNN卷积神经网络算法模型。通过对数据集进行多轮迭代训练,最后得到一个识别精度较高的模型,并保存为本地的h5格式。然后使用Django开发Web网页端操作界面,实现用户上传一段文本识别其所属的类别。
239 1
【新闻文本分类识别系统】Python+卷积神经网络算法+人工智能+深度学习+计算机毕设项目+Django网页界面平台
|
7月前
|
用python扫描linux开放的端口(3种方式)
这篇文章介绍了三种使用Python实现Linux端口扫描的方法,包括基础版端口扫描、全端口扫描和多线程扫描技术。
193 16
|
6月前
|
Python编程--使用NMAP端口扫描
Python编程--使用NMAP端口扫描
64 1
Python编程--目标IP地址段主机指定端口状态扫描
Python编程--目标IP地址段主机指定端口状态扫描
100 1
【果蔬识别系统】Python+卷积神经网络算法+人工智能+深度学习+计算机毕设项目+Django网页界面平台
【果蔬识别系统】Python+卷积神经网络算法+人工智能+深度学习+计算机毕设项目+Django网页界面平台。果蔬识别系统,本系统使用Python作为主要开发语言,通过收集了12种常见的水果和蔬菜('土豆', '圣女果', '大白菜', '大葱', '梨', '胡萝卜', '芒果', '苹果', '西红柿', '韭菜', '香蕉', '黄瓜'),然后基于TensorFlow库搭建CNN卷积神经网络算法模型,然后对数据集进行训练,最后得到一个识别精度较高的算法模型,然后将其保存为h5格式的本地文件方便后期调用。再使用Django框架搭建Web网页平台操作界面,实现用户上传一张果蔬图片识别其名称。
182 0
【果蔬识别系统】Python+卷积神经网络算法+人工智能+深度学习+计算机毕设项目+Django网页界面平台
Python 网络编程:端口检测与IP解析
本文介绍了使用Python进行网络编程的两个重要技能:检查端口状态和根据IP地址解析主机名。通过`socket`库实现端口扫描和主机名解析的功能,并提供了详细的示例代码。文章最后还展示了如何整合这两部分代码,实现一个简单的命令行端口扫描器,适用于网络故障排查和安全审计。
115 0
|
8月前
|
炫酷!纯Python开发LOL英雄信息查询平台
炫酷!纯Python开发LOL英雄信息查询平台
78 2

热门文章

最新文章