Beispiel #1
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    # 获取用户名,用于发送短信验证码显示用户名
    # username = flask.request.args.get('username')
    if not telephone:
        return xtjson.json_params_error(message=u'必须指定手机号码!')
    if xtcache.get(telephone):
        return xtjson.json_params_error(message=u'验证码已发送,请1分钟后重复发送!')
    # if not username:
    #     return xtjson.json_params_error(message=u'必须输入用户名!')
    # 阿里大于APP_KEY及APP_SECRET
    app_key = constants.APP_KEY
    app_secret = constants.APP_SECRET
    req = top.setDefaultAppInfo(app_key, app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend = ""
    req.sms_type = 'normal'
    # 签名名称
    req.sms_free_sign_name = constants.SIGN_NAME
    # 随即生成字符串
    captcha = Captcha.gene_text()
    # 设置短信的模板
    req.sms_param = "{code:%s}" % captcha
    # req.sms_param = "{username:%s,code:%s}" % (username, captcha)
    req.rec_num = telephone.decode('utf-8').encode('ascii')
    req.sms_template_code = constants.TEMPLATE_CODE
    try:
        resp = req.getResponse()
        xtcache.set(telephone, captcha)
        return xtjson.json_result()
    except Exception, e:
        print e
        return xtjson.json_server_error()
Beispiel #2
0
def send_message():
    phone = request.args.get('phone')
    if not phone:
        return xtjson.json_params_error(message=u'必须指定一个手机号码')
    if xtcache.get(phone):
        return xtjson.json_params_error(message=u'验证码已发送,60秒内有效')

    app_key = contants.APP_KEY
    app_secret = contants.APP_SECRET
    req = top.setDefaultAppInfo(app_key,app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend=""
    req.sms_type='normal'
    req.sms_free_sign_name = contants.ALIDAYU_SIGN_NAME
    graph_captcha = captcha.gene_text()
    req.sms_param = "{code:%s}"% graph_captcha
    req.rec_num = phone.decode('utf-8').encode('ascii')
    req.sms_template_code = contants.ALIDAYU_TEMPLATE_CODE
    try:
        resp = req.getResponse()
        xtcache.set(phone,graph_captcha)
        print xtcache.get(graph_captcha)
        return xtjson.json_result()
    except Exception,e:
        print e
        return xtjson.json_server_error()
Beispiel #3
0
def telphone_captcha():
    telphone = flask.request.args.get('telphone')

    if not telphone:
        return xtjson.json_params_error(message=u'请输入手机号码!')

    if xtcache.get(telphone):
        return xtjson.json_params_error(message=u'该手机号码已经申请过验证码,请在10分钟后再试!')

    telphone_captcha = xtcaptcha.Captcha.gene_text()
    xtcache.set(telphone,telphone_captcha,timeout=600) # 10分钟过期

    app_key = ''
    app_secret = ''
    req = top.setDefaultAppInfo(app_key,app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend = ""
    req.sms_type = 'normal'
    req.sms_free_sign_name = 'python论坛'
    req.sms_param = "{code:'%s'}" % telphone_captcha
    req.rec_num = telphone.decode('utf-8').encode('ascii')
    req.sms_template_code = 'SMS_37105066'
    try:
        resp = req.getResponse()
        return xtjson.json_result()
    except Exception,e:
        return xtjson.json_params_error(message=u'短信发送太频繁')
Beispiel #4
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    if not telephone:
        return xtjson.json_params_error(message=u'手机号不能为空!')
    p2 = re.compile('^0\d{2,3}\d{7,8}$|^1[3587]\d{9}$|^147\d{8}')
    phonematch = p2.match(telephone)
    if not phonematch:
        return xtjson.json_params_error(message=u'手机号格式错误')
    tel = FrontUser.query.filter_by(telephone=telephone).first()
    if tel:
        return xtjson.json_params_error(message=u'该手机号已被注册,请勿重复注册')
    if xtcache.get(telephone):
        return xtjson.json_params_error(message=u'验证码已发送,请于1分钟后重新发送')
    app_key = constants.ALIDAYU_APP_KEY
    app_secret = constants.ALIDAYU_APP_SECRET
    req = top.setDefaultAppInfo(app_key, app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend = ""
    req.sms_type = 'normal'
    req.sms_free_sign_name = constants.ALIDAYU_SIGN_NAME
    # 给模版的参数
    captcha = Captcha.gene_text()
    req.sms_param = "{code:'%s'}" % captcha
    req.rec_num = telephone.decode('utf-8').encode('ascii')
    req.sms_template_code = constants.ALIDAYU_TEMPLATE_CODE
    try:
        resp = req.getResponse()
        xtcache.set(telephone, captcha)
        return xtjson.json_result()
    except Exception, e:
        print e
        return xtjson.json_server_error()
Beispiel #5
0
def mail_captcha():
    email = flask.request.args.get('email')
    if xtcache.get(email):
        return xtjson.json_params_error(u'已经给该邮箱发送验证码,请勿重复发送!')
    captcha = Captcha.gene_text()
    if xqmail.send_mail(subject=u'论坛验证码',receivers=email,body=u'您的验证码为:'+captcha+u',请您注意保密'):
        xtcache.set(email,captcha)
        return xtjson.json_result()
    else:
        return xtjson.json_server_error()
Beispiel #6
0
def graph_captcha():
    # 验证码和图片
    text, image = Captcha.gene_code()
    out = StringIO()
    image.save(out, 'png')
    # 将StringIO的指针指向开始的位置
    out.seek(0)
    # 把图片流给读出来
    response = flask.make_response(out.read())
    # 指定响应的类型
    response.content_type = 'image/png'
    xtcache.set(text.lower(), text.lower(), timeout=2 * 60)
    return response
Beispiel #7
0
def graph_captcha():
    text,image = captcha.gene_code()
    # StringIO相当于是一个管道
    out = StringIO()
    # 把image塞到StingIO这个管道中
    image.save(out,'png')
    # 将StringIO的指针指向开始的位置
    out.seek(0)
    # 生成一个响应对象,out.read是把图片流给读出来
    response = flask.make_response(out.read())
    # 指定响应的类型
    response.content_type = 'image/png'
    xtcache.set(text.lower(),text.lower(),timeout=60)
    return response
Beispiel #8
0
def graph_captcha():
    # 验证码过期时间,默认2分钟
    timeout = flask.request.args.get('timeout', 2, int)
    # print 'timeout:', timeout
    text, image = Captcha.gene_code()
    out = StringIO()  # StringIO相当于是一个管道
    image.save(out, 'png')  # 把image塞到StingIO这个管道中
    out.seek(0)  # 将StringIO的指针指向开始的位置

    # 生成一个响应对象,out.read是把图片流给读出来
    response = flask.make_response(out.read())
    # 指定响应的类型
    response.content_type = 'image/png'
    xtcache.set(text.lower(), text.lower(), timeout=timeout * 60)
    return response
Beispiel #9
0
def mail_captcha():
    email = flask.request.args.get('email')
    if xtcache.get(email):
        return xtjson.json_params_error(u'该邮箱已经发送验证码了!')

    source = list(string.letters)
    for x in xrange(0, 10):
        source.append(str(x))
    captcha_list = random.sample(source, 4)
    captcha = ''.join(captcha_list)
    if xtmail.send_mail(subject=u'潭州Python学院邮件验证码',
                        receivers=email,
                        body=u'邮箱验证码是:' + captcha):
        xtcache.set(email, captcha)
        return xtjson.json_result()
    else:
        return xtjson.json_server_error()
Beispiel #10
0
def email_captcha():
    email = flask.request.args.get('email')

    if not email:
        return xtjson.json_params_error(message=u'请输入邮箱!')

    if xtcache.get(email):
        return xtjson.json_params_error(message=u'该邮箱已经申请验证码!')

    email_captcha = xtcaptcha.Captcha.gene_text()
    xtcache.set(email,email_captcha,timeout=60)

    # 发送邮件
    xtmail = XTMail(mail_type=XTMailType.MAIL_CAPTCHA)
    xtmail.send_mail(email)


    return xtjson.json_result()
Beispiel #11
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    if not telephone:
        return xtjson.json_params_error(message='必须指定手机号码!')

    if xtcache.get(telephone):
        return xtjson.json_params_error(message='1分钟内请勿重复发送!')

    code = Captcha.gene_text_num()
    # /accoutn/sms_captcha/?telephone=12345678900
    # telephone = flask.request.form.get('telephone')
    result = aliyun.send_sms(telephone, code=code)
    print(result)
    if result:
        xtcache.set(telephone, code)
        return xtjson.json_result()
    else:
        return xtjson.json_server_error()
Beispiel #12
0
def mail_captcha():
    # mail_captcha/[email protected]/
    # 查询字符串 /mail_captcha/[email protected]
    email = flask.request.args.get('email')

    if xtcache.get(email):
        return xtjson.json_params_error(message=u'该邮箱已经发送验证码了!')

    source = list(string.letters)
    for x in xrange(0, 10):
        source.append(str(x))
    captcha_list = random.sample(source, 4)
    captcha = ''.join(captcha_list)

    if xtmail.send_mail(subject=u'周杰伦发来的邮箱验证码', receivers=email, body=u'邮箱验证码是:'+captcha):
        # 为了下次可以验证邮箱和验证码
        # 为了防止用户不断的刷新这个接口
        xtcache.set(email, captcha)
        return xtjson.json_result()
    else:
        return xtjson.json_params_error(message=u'服务器错误')
Beispiel #13
0
def mail_captcha():
    # /mail_captcha/[email protected]/
    # /mail_captcha/[email protected]
    email = flask.request.args.get('email')

    if xtcache.get(email):
        return xtjson.json_params_error(u'该邮箱已经发送验证码了!')

    source = list(string.ascii_letters)
    for x in range(0, 10):
        source.append(str(x))
    captcha_list = random.sample(source, 4)
    captcha = ''.join(captcha_list)

    if xtmail.send_mail(subject=u'知了课堂Python学院邮件验证码',
                        receivers=email,
                        body=u'邮箱验证码是:' + captcha):
        # 1. 为了下次可以验证邮箱和验证码
        # 2. 为了防止用户不断的刷这个接口
        xtcache.set(email, captcha)
        return xtjson.json_result()
    else:
        return xtjson.json_server_error()
Beispiel #14
0
def sms_captcha():
    telephone = flask.request.args.get('telephone')
    if not telephone:
        return xtjson.json_params_error(message=u'必须指定手机号码!')

    if xtcache.get(telephone):
        return xtjson.json_params_error(message=u'验证码已发送,请1分钟后重复发送!')

    app_key = constants.ALIDAYU_APP_KEY
    app_secret = constants.ALIDAYU_APP_SECRET
    req = AlibabaAliqinFcSmsNumSendRequest(app_key, app_secret)
    req.extend = ""
    req.sms_type = 'normal'
    req.sms_free_sign_name = constants.ALIDAYU_SIGN_NAME
    captcha = Captcha.gene_text()
    req.sms_param = "{code:%s}" % captcha
    req.rec_num = telephone
    req.sms_template_code = constants.ALIDAYU_TEMPLATE_CODE
    try:
        resp = req.getResponse()
        xtcache.set(telephone, captcha)
        return xtjson.json_result()
    except Exception as e:
        return xtjson.json_server_error()
Beispiel #15
0
    def gene_code(cls):
        width, height = cls.size  #宽和高
        image = Image.new('RGBA', (width, height), cls.bgcolor)  #创建图片
        font = ImageFont.truetype(cls.font_path, cls.fontsize)  #验证码的字体
        draw = ImageDraw.Draw(image)  #创建画笔
        text = cls.gene_text()  #生成字符串
        font_width, font_height = font.getsize(text)
        draw.text(((width - font_width) / 2, (height - font_height) / 2),
                  text,
                  font=font,
                  fill=cls.fontcolor)  #填充字符串
        # 如果需要绘制干扰线
        if cls.draw_line:
            # 遍历line_number次,就是画line_number根线条
            for x in xrange(0, cls.line_number):
                cls.__gene_line(draw, width, height)
        # 如果需要绘制噪点
        if cls.draw_point:
            cls.__gene_points(draw, 10, width, height)

        # 设置缓存
        xtcache.set(text.lower(), text.lower())

        return (text, image)
Beispiel #16
0
    captcha = Captcha.gene_text()
    app_key = constants.ALIDAYU_APP_KEY
    app_secret = constants.ALIDAYU_APP_SECRET
    req = top.setDefaultAppInfo(app_key, app_secret)
    req = top.api.AlibabaAliqinFcSmsNumSendRequest()
    req.extend = ""
    req.sms_type = 'normal'
    req.sms_free_sign_name = constants.ALIDAYU_APP_SIGN_NAME
    req.sms_param = constants.ALIDAYU_APP_PARAM % ('Jay', captcha)
    req.rec_num = telephone.decode('utf-8').encode('ascii')
    req.sms_template_code = constants.ALIDAYU_APP_TEMPLATE_CODE
    try:
        # print '短信验证码是:', captcha
        # 设置缓存
        xtcache.set(telephone, captcha)
        resp = req.getResponse()
        return xtjson.json_result()
    except Exception, e:
        # print e
        if e.submsg == u'触发业务流控':
            return xtjson.json_method_error(message=u'发送验证码过于频繁,请于一小时后再注册!')

        return xtjson.json_method_error(message=u'系统繁忙')


# 图片验证码
@bp.route('/graph_captcha/')
def graph_captcha():
    # 验证码过期时间,默认2分钟
    timeout = flask.request.args.get('timeout', 2, int)
Beispiel #17
0
 def __send_captcha_mail__(self):
     msg = Message(u'Python潭州学院论坛邮箱验证码', recipients=self.emails)
     captcha = Captcha.gene_text()
     xtcache.set(captcha.lower(), captcha.lower())
     msg.body = u'邮箱验证码是:%s' % captcha
     mail.send(msg)