python 自定义Server酱模块编写
Posted 活到学到
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 自定义Server酱模块编写相关的知识,希望对你有一定的参考价值。
import datetime
import json
import requests
from exception import AsstException #自定义
from log import logger #自定义
class Messenger(object):
def __init__(self, sc_key):
if not sc_key:
raise AsstException(‘sc_key can not be empty‘)
self.sc_key = sc_key
def send(self, text, desp=‘‘):
if not text.strip():
logger.error(‘Text of message is empty!‘)
return
now_time = str(datetime.datetime.now())
desp = ‘[{0}]‘.format(now_time) if not desp else ‘{0} [{1}]‘.format(desp, now_time)
try:
resp = requests.get(
‘https://sc.ftqq.com/{}.send?text={}&desp={}‘.format(self.sc_key, text, desp)
)
resp_json = json.loads(resp.text)
if resp_json.get(‘errno‘) == 0:
logger.info(‘Message sent successfully [text: %s, desp: %s]‘, text, desp)
else:
logger.error(‘Fail to send message, reason: %s‘, resp.text)
except requests.exceptions.RequestException as req_error:
logger.error(‘Request error: %s‘, req_error)
except Exception as e:
logger.error(‘Fail to send message [text: %s, desp: %s]: %s‘, text, desp, e)
if __name__ == ‘__main__‘:
from config import global_config
messenger = Messenger(global_config.get(‘messenger‘, ‘sckey‘))
messenger.send(text=‘标题‘, desp=‘内容‘)
以上是关于python 自定义Server酱模块编写的主要内容,如果未能解决你的问题,请参考以下文章