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

相关文章
|
9月前
|
Python
Python网络编程基础(Socket编程)绑定地址和端口
【4月更文挑战第9天】在UDP服务器编程中,我们首先需要创建一个UDP套接字,然后绑定一个本地地址和端口,以便客户端可以通过这个地址和端口与我们的服务器进行通信。下面,我们将详细讲解如何绑定地址和端口。
|
Python
python 子域名 分解路径 将子域名的路径提取为字典
python 子域名 分解路径 将子域名的路径提取为字典
80 0
|
9月前
|
数据安全/隐私保护 时序数据库
InfluxData【部署 03】时序数据库 InfluxDB 离线安装配置使用(下载+安装+端口绑定+管理员用户创建+开启密码认证+开机自启配置)完整流程实例分享
InfluxData【部署 03】时序数据库 InfluxDB 离线安装配置使用(下载+安装+端口绑定+管理员用户创建+开启密码认证+开机自启配置)完整流程实例分享
515 0
|
7月前
|
Shell 索引 Python
Python的延迟绑定是什么?
Python中的延迟绑定指的是在嵌套函数中,内部函数在被调用时才绑定外部函数的变量,而不是在定义时绑定。这可能导致意外行为,因为变量的值是在函数调用时决定的。
|
7月前
|
运维 Python Windows
如何通过Python脚本查找并终止占用指定端口的进程
在日常的开发和运维过程中,某些端口被意外占用是一个常见的问题。这种情况可能导致服务无法启动或冲突。本文将介绍如何通过Python脚本查找并终止占用指定端口的进程,以确保系统的正常运行。
|
8月前
|
Python
python多态 , 绑定方法与内置方法
python多态 , 绑定方法与内置方法
|
9月前
|
网络协议
TCP和UDP可以绑定同样的端口吗?
TCP和UDP可以绑定同样的端口吗?
|
9月前
|
网络协议
绑定地址和端口
【4月更文挑战第4天】创建socket对象后,需将其绑定到特定地址和端口。根据服务器需求,地址可选localhost(仅本机服务)、实际IP地址(公开服务)或空字符串(所有地址)。端口号应避开0-1023的保留范围。使用`bind()`方法绑定地址和端口,如`sock.bind(('', 12345))`。绑定可能遇到错误,需用异常处理机制捕获,确保程序稳定。
|
9月前
|
JavaScript 前端开发 网络协议
Python之JavaScript逆向系列——1、URL——域名
Python之JavaScript逆向系列——1、URL——域名
67 0
|
9月前
|
开发工具 Android开发 iOS开发
​ 2023年APP备案操作教程 阿里云APP备案试列 APP公钥sha1签名获取方法阿里云新增APP备案操作教程西部数码、腾讯云、新网、等等其他域名备案信息系统操作类似;核心要点:A,域
​ 2023年APP备案操作教程 阿里云APP备案试列 APP公钥sha1签名获取方法阿里云新增APP备案操作教程西部数码、腾讯云、新网、等等其他域名备案信息系统操作类似;核心要点:A,域
423 0

热门文章

最新文章