腾讯云平台绑定域名端口是要一个个绑,比较麻烦,之前看了一下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
相关阅读:
Python脚本获取Linux系统信息
通过Shell、Python脚本管理KVM虚拟机