示例#1
0
from tornado.escape import json_encode, json_decode
from tool import applog
from config import CONFIG_REDIS_URI, CONFIG_REDIS_MAXCONNECT, CONFIG_REDIS_MINCONNECT
import aioredis

log = applog.get_log('tool.async_redis_pool')


class RedisOperate(object):
    def __init__(self):
        self.obj_pool = None

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(RedisOperate, "_instance"):
            RedisOperate._instance = RedisOperate(*args, **kwargs)
        return RedisOperate._instance

    async def create_redis_pool(self):
        log.info('create redis connection pool...')
        self.obj_pool = await aioredis.create_pool(
            CONFIG_REDIS_URI,
            minsize=CONFIG_REDIS_MINCONNECT,
            maxsize=CONFIG_REDIS_MAXCONNECT)

    async def execute(self, *arg):
        # 总的执行方法
        if self.obj_pool is None:
            await self.create_redis_pool()
        try:
            return await self.obj_pool.execute(*arg)
示例#2
0
import json
from urllib import parse

import tornado.web
import urllib.parse
import tornado.options
import time

from tornado.escape import json_decode
from tool import applog, function
from tool.async_redis_pool import RedisOperate
from config import SENSTIVE_WORD, API_SECURITY_CHECK_OPEN, API_SECURITY_SECONDS, API_SECURITY_SECRET

log = applog.get_log('webhandler.basehandler')
log.setLevel('INFO')
redisdb = RedisOperate().instance()
from motor.motor_asyncio import AsyncIOMotorClient

connection = AsyncIOMotorClient("127.0.0.1", 27017)


class BaseHandler(tornado.web.RequestHandler):
    def options(self):
        # no body
        self.set_status(204)
        self.finish()

    def get_current_user(self):
        ''' 验证用户是否登录 '''
        user = self.get_secure_cookie("cmsadmincookie")
        if user:
示例#3
0
from random import randint
from tool.asynchttp import async_http_response
from tool import applog
from sms.buildsmsurl import AliyunSMSAdapter

log = applog.get_log('sms.aliyunsms')


async def send_aliyun_send_sms(phone, t_code):
    # 发送短信
    verifi_code = randint(100000, 999999)
    sms = AliyunSMSAdapter()
    url = sms.get_signature_url(phone, t_code, {"code": verifi_code})
    success, result = await async_http_response(url, datatype='json')
    if success:
        if result.get('Code') == 'OK':
            log.debug("url:{}\nresponse:{}".format(url, result))
            return True, result.get('RequestId'), verifi_code
        else:
            log.error("url:{},errorcode:{},RequestId:{}".format(
                url, result.get('Code'), result.get('RequestId')))
            return False, result.get('RequestId'), result.get('Code')
    else:
        from tornado.escape import json_decode
        result = json_decode(result)
        log.error("url:{},errorcode:{},RequestId:{}".format(
            url, result.get('Code'), result.get('RequestId')))
        return False, result.get('RequestId'), result.get('Code')
    # {
    #     "Message":"OK",
    #     "RequestId":"2184201F-BFB3-446B-B1F2-C746B7BF0657",
示例#4
0
from asgiref.sync import sync_to_async
from tornado.escape import json_decode
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from config import MONGODB
from tool import applog
from tool.asynchttp import async_http_response
from webhandler.basehandler import BaseHandler

# conn = pymssql.connect(host='192.168.32.24', port='1433', user='******', password='******', database='TaoKe')
# cur = conn.cursor()
from pymongo import MongoClient

# client = MongoClient('127.0.0.1', 27017)
from motor.motor_asyncio import AsyncIOMotorClient

log = applog.get_log('webhandler.count')
log.setLevel('INFO')
# client = AsyncIOMotorClient('mongodb://*****:*****@39.105.179.250:27017/spider')
client = AsyncIOMotorClient('mongodb://{}:{}@{}:{}/{}'.format(
    MONGODB['user'],
    MONGODB['pwd'],
    MONGODB['host'],
    MONGODB['port'],
    MONGODB['db'],
))
# client = MongoClient('39.105.179.250', 27017)
db = client.spider

# db.authenticate('testmongo', 'testmongo123')

示例#5
0
import logging
from tornado.httpclient import AsyncHTTPClient,HTTPRequest
from tornado.escape import json_decode
from tool import applog

log = applog.get_log('tool.asynchttp')


AsyncHTTPClient.configure(
    None, defaults={'User-Agent': 'AlibabaCloud (Windows 10;AMD64) Python/3.6.6 Core/2.13.3 python-requests/2.18.3', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'x-sdk-invoke-type': 'common', 'x-sdk-client': 'python/2.0.0', 'Content-Length': '0'})

# log.warning('dfasd')

async def async_http_response(url, method='GET', post_data=None, headers={}, datatype='str'):
    '''访问公共接口, 返回值(true,str)'''
    # log.debug('request,method:{},url:{},post_data:{}'.format(method, url, post_data))
    # post_str = "&".join([k+'='+str(v) for k,v in post_data.items()])
    reqobj = HTTPRequest(url, method=method, body=post_data, headers=headers, 
        connect_timeout=10, request_timeout=10, validate_cert=False)

    response = await AsyncHTTPClient().fetch(reqobj, raise_error=False)
    rsp_str = response.body.decode("utf-8") if response.body is not None else ''
    log.debug('\nrequest,url:{},post:{}\nresponse,code:{}'.format(url, post_data, response.code))
    if response.code == 200:
        try:
            return (True, json_decode(rsp_str)) if datatype == 'json' else (True, rsp_str)
        except Exception as e:
            log.error("url:{},post:{}\nerror:{}\nresponse:\n{}".format(url,post_data, e, rsp_str))
            return False,'json 解码错误'
    else:
        log.warning("url:{},post:{}\ncode:{}\nerror:{}\nresponse:\n{}".format(url,post_data, response.code, response.error, rsp_str))
示例#6
0
'''
dbpool 数据库操作类
'''
import aiomysql
from tool import applog
from config import CONFIG_MYSQL

log = applog.get_log('tool.dbpool')


class DbOperate(object):
    def __init__(self):
        self.__pool = None

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(DbOperate, "_instance"):
            DbOperate._instance = DbOperate(*args, **kwargs)
        return DbOperate._instance

    async def create_db_pool(self):
        log.info('create database connection pool...')
        if isinstance(self.__pool, aiomysql.pool.Pool):
            return self.__pool
        try:
            obj_pool = await aiomysql.create_pool(
                host=CONFIG_MYSQL.get('host', '127.0.0.1'),
                port=CONFIG_MYSQL.get('port', 3306),
                user=CONFIG_MYSQL['user'],
                password=CONFIG_MYSQL['password'],
                db=CONFIG_MYSQL['db'],