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

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
3月前
|
Web App开发 数据采集 自然语言处理
python脚本抢各大平台大额优惠卷
python脚本抢各大平台大额优惠卷
56 0
|
6月前
|
Python
python 子域名 分解路径 将子域名的路径提取为字典
python 子域名 分解路径 将子域名的路径提取为字典
45 0
|
6月前
|
搜索推荐 算法 前端开发
旅游管理与推荐系统Python+Django网页平台+协同过滤推荐算法
旅游管理与推荐系统Python+Django网页平台+协同过滤推荐算法
157 0
|
5月前
|
机器学习/深度学习 算法 TensorFlow
【Python机器学习】梯度下降法的讲解和求解方程、线性回归实战(Tensorflow、MindSpore平台 附源码)
【Python机器学习】梯度下降法的讲解和求解方程、线性回归实战(Tensorflow、MindSpore平台 附源码)
96 0
|
29天前
|
JavaScript 搜索推荐 前端开发
音乐发现平台:借助Python和Vue构建个性化音乐推荐系统
【4月更文挑战第11天】本文介绍了如何使用Python和Vue.js构建个性化音乐推荐系统。首先确保安装Python、Node.js、数据库系统和Git。后端可选择Flask或Django搭建RESTful API,处理歌曲数据。前端利用Vue.js创建用户界面,结合Vue CLI、Vuex和Vue Router实现功能丰富的SPA。通过Vuex管理状态,Axios与后端通信。这种前后端分离的架构利于协作和系统扩展,助力打造定制化音乐体验。
|
1月前
|
前端开发 JavaScript API
打造在线教育平台:Python后端与Vue前端的完美融合
【4月更文挑战第10天】本文探讨了如何利用Python后端与Vue前端构建功能强大、用户体验良好的在线教育平台。通过Python的Django或Flask框架搭建稳定API服务,结合Vue.js的组件化和数据驱动特性创建高效用户界面。步骤包括确定平台需求、构建后端、创建前端、前后端交互、实现认证授权、优化体验以及测试部署。这种技术融合为在线教育市场提供了一个高效、可扩展的解决方案,适应未来教育平台的智能化、个性化和多元化趋势。
|
2月前
|
存储 数据可视化 API
Python项目开发:Flask基于Python的天气数据可视化平台
Python项目开发:Flask基于Python的天气数据可视化平台
64 0
|
2月前
|
开发框架 人工智能 前端开发
使用Python、Django和Bootstrap构建在线教育平台
使用Python、Django和Bootstrap构建在线教育平台
47 0
|
3月前
|
JavaScript 前端开发 网络协议
Python之JavaScript逆向系列——1、URL——域名
Python之JavaScript逆向系列——1、URL——域名
27 0
|
4月前
|
开发工具 Android开发 iOS开发
​ 2023年APP备案操作教程 阿里云APP备案试列 APP公钥sha1签名获取方法阿里云新增APP备案操作教程西部数码、腾讯云、新网、等等其他域名备案信息系统操作类似;核心要点:A,域
​ 2023年APP备案操作教程 阿里云APP备案试列 APP公钥sha1签名获取方法阿里云新增APP备案操作教程西部数码、腾讯云、新网、等等其他域名备案信息系统操作类似;核心要点:A,域
244 0