Example #1
0
def sms_captcha():
    '''
    实现:
    1. telephone
    2. timestamp
    3. md5(ts+telephone+salt)
    :return:
    '''
    # 1. 申明验证表单验证对象
    form = SMSCaptchaForm(request.form)
    # 2. 通过验证
    if form.validate():
        # 2.1 拿到手机号
        telephone = form.telephone.data
        # 2.2 生成验证码
        captcha = Captcha.gene_text(number=4)
        print('发送的短信验证码是:', captcha)
        # 2.3 发送验证码,成功时:
        if alidayu.send_sms(telephone, code=captcha):

            # ⚠️:这里将验证码保存在缓存服务器中
            zlcache.set(telephone, captcha, timeout=60)

            return restful.success()
        # 发送验证码,失败时:
        else:
            return restful.params_error()
    else:
        return restful.params_error(message='参数错误!')
Example #2
0
File: views.py Project: Joynice/eog
def email_captcha():
    email = request.args.get('email')
    params = request.referrer.split('/')[-2]
    print(email)
    print(params)
    regex = '^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$'
    if not re.match(regex, email):
        return restful.params_error('请输入正确格式的邮箱!')
    if not email:
        return restful.params_error('请传递邮箱参数!')
    elif params == 'signup' and User.objects(email=email).first():
        return restful.params_error('邮箱已经被注册,请更换邮箱!')
    elif User.objects(email=email).first() and (params == 'resetpwd'
                                                or params == 'resetmail'):
        return restful.params_error('该邮箱已经被注册,请更换邮箱!')
    else:
        source = list(string.ascii_letters)
        source.extend(map(lambda x: str(x), range(0, 10)))
        captcha = "".join(random.sample(source, 6))
        print(captcha)
        message = Message('专家值守平台邮箱验证码',
                          recipients=[email],
                          body='您的验证码是:{}'.format(captcha))
        print(message)
    try:
        mail.send(message)
    except Exception as e:
        return restful.server_error(message='可能邮箱不存在!请检查后再试')
    zlcache.set(email, captcha)
    return restful.success()
Example #3
0
def graph_captcha():
    text, image = Captcha.gene_graph_captcha()
    zlcache.set(text.lower(), text.lower())
    out = BytesIO()
    image.save(out, 'png')
    out.seek(0)
    resp = make_response(out.read())
    resp.content_type = 'image/png'
    return resp
Example #4
0
def graph_captcha():
    text, image = Captcha.gene_graph_captcha()
    zlcache.set(text, text)
    out = BytesIO()
    image.save(out, "png")
    out.seek(0)
    res = make_response(out.read())
    res.content_type = "image/png"
    return res
def graph_captcha():
    captcha, image = Captcha.gene_graph_captcha()
    print('图形验证码是:', captcha)  # 打印出图形验证码
    zlcache.set(captcha.lower(), captcha.lower())  # 把验证码添加到memcached缓存中
    out = BytesIO()
    image.save(out, 'png')
    out.seek(0)
    resp = make_response(out.read())
    resp.content_type = 'image/png'
    return resp
Example #6
0
def sms_captchas():
    telephone = request.args.get("telephone")
    if not telephone:
        return restful.paramError(message="手机号不能为空")

    code = aliyunAPI.get_code(1)
    print("验证码:",code)
    res = aliyunAPI.send_sms(telephone=telephone,code=code)
    zlcache.set(telephone, code)
    return res
Example #7
0
def sms_captcha():
    form = SMSCaptchaForm(request.form)
    if form.validate():
        telephone = form.telephone.data
        captcha = Captcha.gene_text(number=4)
        send_sms_captcha(telephone, captcha)
        zlcache.set(telephone, captcha)
        return restful.success()
    else:
        return restful.params_error(message='参数错误!')
Example #8
0
def sms_captcha():
    form = SMSCaptchaForm(request.form)
    if form.validate():
        telephone = form.telephone.data
        capt = Captcha.gene_text(number=4)
        # 发送验证码
        zlcache.set(telephone, capt)
        print("向手机{}发送验证码:{}".format(telephone, capt))
        return restful.success()
    else:
        return restful.params_error("手机号码格式有误")
Example #9
0
def graph_captcha():
    # 获取验证码
    text, image = Captcha.gene_graph_captcha()
    zlcache.set(text.lower(), text.lower())
    # BytesIO: 自截留
    out = BytesIO()
    image.save(out, 'png')
    out.seek(0)  # 将指针返回开头,save过后指针位置在最后
    resp = make_response(out.read())
    resp.content_type = 'image/png'
    return resp
Example #10
0
def graph_captcha():
    #获取验证码
    text,image=Captcha.gene_graph_captcha()
    zlcache.set(text.replace(" ","").lower(),text.replace(" ","").lower()) #text.replece(" ","")去掉空格
    #ByteIo:字节流 (必须用二进制流的形式才能在网络上传输) 保存在内存中
    out=BytesIO()
    image.save(out,'png') #保存的格式是png
    out.seek(0) #把文件流的指针放到最开始的位置
    resp=make_response(out.read()) #创建response对象
    resp.content_type='image/png'#response的数据类型
    return resp
Example #11
0
def graph_captcha():
    # 获取验证码
    text, image = Captcha.gene_graph_captcha()
    zlcache.set('graph_captcha', text)
    # BytesIO 自截留,二进制流数据
    out = BytesIO()
    image.save(out, 'png')
    out.seek(0)
    resp = make_response(out.read())
    resp.content_type = 'image/png'
    return resp
Example #12
0
def graph_captcha():
    #获取验证码
    text, image = Captcha.gene_graph_captcha()
    zlcache.set(text.lower(),
                text.lower())  #图形验证吗存储到memcached,key和value都是验证码本身并变为小写存储
    #字节流
    out = BytesIO()
    image.save(out, 'png')
    out.seek(0)
    resp = make_response(out.read())
    resp.content_type = 'image/png'
    return resp
Example #13
0
def sms_captcha():
    form = SMSCaptchaForm(request.form)
    if form.validate():
        telephone = form.telephone.data
        captcha = Captcha.gene_text(4)
        if send_sms_captcha(telephone, captcha):
            zlcache.set(telephone,captcha)
            print('本次的手机验证码是:%s' %captcha)
            return restful.success()
        else:
            zlcache.set(telephone, captcha)
            return restful.params_error(message='参数错误')
    else:
        return restful.params_error(message='参数错误')
Example #14
0
def email_captcha():
    email = request.args.get("email")
    if not email:
        return restful.params_error("请输入邮箱")
    source = list(string.ascii_letters)
    source.extend(map(lambda x: str(x), range(0, 10)))
    captcha = "".join(random.sample(source, 6))
    zlcache.set(email, captcha)
    message = Message("验证码", [email], "您的验证码是:%s" % captcha)
    try:
        mail.send(message)
    except:
        return restful.server_error()
    return restful.success()
Example #15
0
def index():
    # md5(timesleep+telephone+salt)
    form = SMSCaptcha(request.form)
    if form.validate():
        telephone = form.telephone.data
        captcha = Captcha.gene_text(number=4)
        print('sms is {}'.format(captcha))
        if alidayu.send_sms(telephone, code=captcha):
            zlcache.set(telephone, captcha)
            return restful.success()
        # return restful.params_error(message='短信验证码发送失败')
        zlcache.set(telephone, captcha)
        return restful.success()
    return restful.params_error(message='参数错误')
Example #16
0
def graph_captcha():
    # 获取验证码
    text,image=Captcha.gene_graph_captcha()
    print('图形验证码:%s'%text)
    zlcache.set(text.lower(),text.lower())
    # BytesIO:字节流  创建二进制流对象
    out=BytesIO()
    image.save(out,'png')
    # 将文件指针放到最开始位置,方便下面读取.如果指针在最末尾,读取不到文件
    out.seek(0)
    resp=make_response(out.read())
    resp.content_type='image/png'
    # resp.content_type='application/octet-stream'
    return resp
Example #17
0
def sms_captcha():
    #teltphone
    #timestamp
    #md5(timestamp+teltphone+salt)
    form = SMSCaptchaForm(request.form)
    if form.validate():
        telephone = form.telephone.data
        captcha = Captcha.gene_text(number=4)
        zlcache.set(telephone, captcha)
        #感觉此处应该加delay老师没加
        send_sms_captcha.delay(telephone, captcha)
        return restful.success()
    else:
        return restful.params_error(message='参数错误!')
Example #18
0
def email_captcha():
    # 0. 邮箱验证:正则校验
    # 通过验证:
    email = request.args.get('email')
    import re
    if re.match(
            "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}| \
            [0-9]{1,3})(\\]?)$", email) is not None:
        # /email_capicha/[email protected] - 通过查询字符串的形式将邮箱传递到后台
        # 1. 查询字符串
        email = request.args.get('email')
        if not email:
            return restful.params_error('请传递邮箱参数!')

        # 2. 产生验证码
        # 2.1 a-zA-Z的字符串
        source = list(string.ascii_letters)

        # 2.2 将一个列表的值更新到另一个列表中,利用list.extend()
        # 方法1
        # source.extend(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])

        # 方法2
        # map(func,obj) 将obj(需要可迭代的对象)的数据传递到函数中,然后处理后再返回
        # lambda函数:匿名函数
        # lambda x:str(x) 定义一个匿名函数,变量为x,处理方式为将x传入到str()中,进行字符串转义
        source.extend(map(lambda x: str(x), (range(0, 10))))

        # 2.3 随机采样
        # sample 采样器,从source中随机选择6个,返回值为列表
        list_captcha = random.sample(source, 6)

        # 将字符串转换成列表
        captcha = "".join(list_captcha)

        # 3.给这个邮箱发送邮件
        message = Message('武汉柠檬班论坛邮箱验证码',
                          recipients=[email],
                          body='您的验证码是:%s' % captcha)
        try:
            mail.send(message)
        except Exception as e:
            return restful.server_error()
        # 4. 存验证码,key=email,value=captcha
        zlcache.set(email, captcha)

        return restful.success()
    else:
        return restful.params_error(message='请输入正确的邮箱格式!')
Example #19
0
def sms_captcha():
    form = SMSCaptchaForm(request.form)
    if form.validate():
        telephone = form.telephone.data
        captcha = Captcha.gene_text(number=4)
        print('发送的短信验证码是:',captcha)
        if alidayu.send_sms(telephone,code=captcha):
            zlcache.set(telephone,captcha)
            return restful.success()
        else:
            # return restful.params_error()
            zlcache.set(telephone,captcha)
            return restful.success()
    else:
        return restful.params_error(message='参数错误!')
Example #20
0
def email_captcha():
    email = request.args.get('email')
    if not email:
        restful.paramas_error('请输入正确的邮箱')
    source = list(string.ascii_letters)
    source.extend(map(lambda x: str(x), range(1, 10)))
    captcha = ''.join(random.sample(source, 6))
    # message=Message('Python论坛邮箱验证码',recipients=[email],body='你的验证码是:%s'%captcha)
    # try:
    #     mail.send(message)
    # except:
    #     return restful.sever_error()
    send_mail.delay('小木虫论坛邮箱验证码', [email], '你的验证码是:%s' % captcha)
    zlcache.set(email, captcha)
    return restful.success()
Example #21
0
def sms_captcha():
    #     telephone+timestamp+salt
    form = SMSCaptchaForm(request.form)
    if form.validate():
        telephone = form.telephone.data
        captcha = Captcha.gene_text(number=4)
        if alidayu.send_sms(telephone, code=captcha):
            zlcache.set(telephone, captcha)  # 验证码保存到缓存中
            return restful.success()
        else:
            # return restful.paramas_error(message='参数错误')
            zlcache.set(telephone, captcha)  # 测试用
            return restful.success()
    else:
        return restful.params_error(message='参数错误')
Example #22
0
def email_captcha():
    from tasks import send_mail
    email = request.args.get('email')
    if not email:
        return field.params_error('请传递邮箱参数!')
    user = User.query.filter_by(email=email).first()
    if user:
        return field.params_error('该邮箱已经注册,请更换邮箱!')
    source = list(string.ascii_letters)
    source.extend(map(lambda x: str(x), range(0, 10)))
    captcha = "".join(random.sample(source, 6))
    print(captcha)
    send_mail.delay('牧羊人邮箱验证码', [email], '您的验证码是:{}'.format(captcha))
    zlcache.set(email, captcha)
    return field.success()
Example #23
0
def graph_captcha():
    text, image = Captcha.gene_graph_captcha()
    #存储图形验证码到服务器
    zlcache.set(text.lower(),text.lower())

    out = BytesIO()
    image.save(out, 'png')
    out.seek(0)
    resp = make_response(out.read())
    # print(resp)
    # resp = Response(resp,mimetype="image/png")
    # resp.context_type = 'image/png'
    resp.headers['Content-Type'] = 'image/jpg'

    return resp
Example #24
0
def email_captcha():
    email = request.args.get('email')
    if not email:
        return '请传递邮箱参数'
    source = list(string.ascii_letters)
    source.extend(str(_) for _ in range(0, 10))
    captcha = ''.join(random.sample(source, 6))
    # message = Message(subject='Python', recipients=[email], body='验证码:{}'.format(captcha))
    # try:
    #     mail.send(message)
    # except:
    #     return restful.server_error()
    send_mail.delay('Python', [email], '验证码:{}'.format(captcha))
    zlcache.set(email, captcha)
    return restful.success()
Example #25
0
def sms_captcha():
    form = SMSCaptchaForm(request.form)
    if form.validate():
        telephone = form.telephone.data
        captcha = Captcha.gene_text(number=4)  #生成4位短信验证码
        print("发送的短信验证码是%s" % captcha)
        if alidayu.send_sms(telephone, code=captcha):
            zlcache.set(telephone,
                        captcha)  #将手机验证码存储到memcached种,telephone为KEY,captcha为值
            return restful.success()
        else:
            #return restful.parames_error("参数错误")
            zlcache.set(telephone, captcha)  ### 为了测试 就算阿里大于发送失败也存储到memcached
            return restful.success()  #用于测这里设置为成功!!!
    else:
        return restful.parames_error(message="参数错误")
Example #26
0
def email_captcha():
    email = request.args.get('email')
    if not email:
        return restful.params_error('请传递邮箱参数')

        captcha = ''.join(random.sample('0123456789', 4))

    message = Message('python论坛邮箱验证',
                      recipients=[email],
                      body='您的验证码是:{}'.format(captcha))
    try:
        mail.send(message)
    except:
        return restful.server_error()
    zlcache.set(email, captcha)
    return restful.success()
Example #27
0
def email_captcha():
    email = request.args.get('email')
    if not email:
        return restful.params_error('请传递邮箱参数!')
    source = list(string.ascii_letters)
    source.extend([str(x) for x in range(10)])
    captcha = ''.join(random.sample(source, 6))
    #指定邮箱发送邮件
    # message = Message('Python论坛验证码',recipients=[email],body='您的验证码内容是:%s' %captcha)
    # try:
    #     mail.send(message)
    # except:
    #     return restful.server_error()
    send_mail.delay('Python论坛验证码', [email], '您的验证码内容是:%s' % captcha)
    zlcache.set(email, captcha)
    return restful.success()
Example #28
0
def send_email_capcha():
    email = request.form.get('email')
    if not email:
        return restful.params_errors("请输入邮箱地址")
    else:
        source = list(string.ascii_letters + string.digits)
        emailCapcha = "".join(random.sample(source, 6))
        message = Message(subject="品联APK管理平台-邮箱验证码",
                          recipients=[email],
                          body="当前验证码为:{}, 该验证码有效期为5分钟".format(emailCapcha),
                          sender=config.MAIL_DEFAULT_SENDER)
        try:
            mail.send(message)
            zlcache.set(email, emailCapcha)
            return restful.success()
        except:
            return restful.server_errors()
Example #29
0
def email_captcha():
    email = request.args.get('email')
    if not email:
        return restful.params_error('请传递邮箱参数')
    source = list(string.ascii_letters)
    source.extend(map(lambda x: str(x), range(10)))
    captcha = "".join(random.sample(source, 6))
    message = Message("Python邮箱验证码", recipients=[email], body="你的验证码是:%s" % captcha)
    # 给邮箱发送邮件
    try:
        mail.send(message)
    except:
        return restful.server_error()
    # send_mail.delay("Python邮箱验证码",[email],"你的验证码是:%s" % captcha)
    zlcache.set(email, captcha)
    print(zlcache.get(email))
    return restful.success()
Example #30
0
def email_captcha():
    email = request.args.get('email')
    if not email:
        return restful.params_error('请传递邮箱参数!')
    source = list(string.ascii_letters)  # 返回小写a~z的字母
    source.extend(map(lambda x: str(x), range(0, 10)))
    captcha = "".join(random.sample(source, 6))  # 生成验证码

    # 给这个邮箱发送邮件
    # message = Message('Python论坛邮箱验证码', recipients=[email], body='您的验证码是:%s' % captcha)
    # try:
    #     mail.send(message)
    # except:
    #     return restful.server_error()
    send_mail('Python论坛邮箱验证码', recipients=[email], body='您的验证码是:%s' % captcha)
    zlcache.set(email, captcha)
    return restful.success()