歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python批量綁定騰訊雲平台CVM域名與端口

Python批量綁定騰訊雲平台CVM域名與端口

日期:2017/3/1 9:52:14   编辑:Linux編程

騰訊雲平台綁定域名端口是要一個個綁,比較麻煩,之前看了一下API,簡單寫了一個綁域名端口的腳本:

ApiLogger.py(這個是輸出日志用的)
#!/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提交信息的基礎方法)
#!/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

Copyright © Linux教程網 All Rights Reserved