Python 常用工具类
博客专区 > james_lz 的博客 > 博客详情
Python 常用工具类
james_lz 发表于5个月前
Python 常用工具类
  • 发表于 5个月前
  • 阅读 89
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

def _create_secret(length=10):
    """
    获取由10位随机大小写字母、数字组成的值
    :param length:
    :return:
    """
    key = ''
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    len_chars = len(chars) - 1
    for i in xrange(length):
        # 每次从chars中随机取一位
        key += chars[random.randint(0, len_chars)]
    return key


def _create_percent():
    """
    创建赢家百分比
    :return:
    """
    return random.uniform(1, 100)


def _get(url, params=None, timeout=5, headers=_get_headers()):
    """

    :param url:
    :param params:
    :param timeout:
    :param headers:
    :return:
    """
    try:
        response = requests.get(url=url, params=params, timeout=timeout, headers=headers)
        response.raise_for_status()
        result = response.json()
    except BaseException as e:
        logger.info(e.message)
        client.captureException()
        result = None
    return result


def _post(url, params=None, json=None, timeout=5, headers=_get_headers()):
    """

    :param url:
    :param params:
    :param json:
    :param timeout:
    :param headers:
    :return:
    """
    try:
        response = requests.post(url=url, data=params, json=json, headers=headers, timeout=timeout)
        response.raise_for_status()
        result = response.json()
    except BaseException as e:
        logger.info(e.message)
        client.captureException()
        result = {'status': False, 'message': ''}
    return result

def valid_signature(params, timeout=settings.SIGNATURE_TIMEOUT):
    """
    
    :param params: 
    :param timeout: 
    :return: 
    """
    if not params or not isinstance(params, dict):
        return False

    signature = params.pop('signature', '')
    app_id = params.get('app_id', '')
    if not signature or not app_id:
        return False

    timestamp = params.get('timestamp', '')
    current_timestamp = int(time.time())
    if current_timestamp - int(timestamp) > timeout:
        return False

    website = Website.obj.get_by_appid(app_id)
    if not website:
        return False

    sign = generate_signature(website.secret_key, params)
    if sign != signature:
        return False

    return True


def generate_signature(secret_key, params):
    """
    生成签名
    :param secret_key: 
    :param params: 
    :return: 
    """
    params['secret_key'] = secret_key
    keys = sorted(params.keys())
    tmp_str = ''
    for key in keys:
        tmp_str = '{tmp_str}{key}={value}'.format(tmp_str=tmp_str, key=key, value=unicode(params.get(key)))
    md5_str = _md5(tmp_str.encode('utf-8'))
    params.pop('secret_key')
    return md5_str


def _md5(str):
    """
    加密
    :param str:
    :return:
    """
    try:
        my_md5 = hashlib.md5()
        my_md5.update(str)
        md5_str = my_md5.hexdigest()
    except BaseException as e:
        return None
    return md5_str.upper()

 

标签: Python
共有 人打赏支持
粉丝 10
博文 135
码字总数 42486
×
james_lz
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: