Python编程django实现同一个ip十分钟内只能注册一次

yipeiwu_com7年前Python基础

很多小伙伴都会有这样的问题,说一个ip地址十分钟内之内注册一次,用来防止用户来重复注册带来不必要的麻烦

逻辑:

取ip,在数据库找ip是否存在,存在判断当前时间和ip上次访问时间之差,小于600不能注册,到登录界面,大于600可以注册,设计一个数据库来存储这个ip地址和访问时间,

class Ip(models.Model):
  ip=models.CharField(max_length=20)
  time=models.DateTimeField()
  class Meta:
    verbose_name = u'访问时间'
    verbose_name_plural = verbose_name
  def __str__(self):
    return self.ip

然后去

  python manage.py makemigrations
  python manage.py migrate

  这样来更新我们的数据库,然后我们运行我们的项目可以在后台看到我们新注册的ip的数据

 我们根据前面的逻辑,可以来设计我们的代码,

from django.views.generic.base import View
from blog.models import Ip
class RegView(View):
  def get(self,request):
    ipreques = request.META['REMOTE_ADDR']
    try:
      ip_c=Ip.objects.get(ip=ipreques)
      if ip_c :
        if (datetime.datetime.now()-ip_c.time).total_seconds()<600:
          return render(request, 'login.html', {'msg': u'10分钟内只能注册一次'})
        ip_c.time=datetime.datetime.now()
        ip_c.save()
        return render(request, 'reg.html')
    except Exception as e:
      new=Ip()
      new.ip=str(ipreques)
      new.time=datetime.datetime.now()
      new.save()
      return render(request, 'reg.html')
  def post(self,request):
    username=request.POST['username']
    if len(getuser(username))<=0:
      return render(request,'reg.html',{'msg':u'用户名应该是6-16组成'})
    passwor1 = request.POST['password']
    passwor2 = request.POST['password1']
    shouj = request.POST['shouji']
    if len(getPhoneNumFromFile(shouj))<=0:
      return render(request, 'reg.html', {'msg':u'手机号格式是否正确'})
    shouji = User.objects.filter(mobile__exact=shouj)
    if shouji:
      return render(request, 'reg.html', {'msg': u'手机号已经存在'})
    youjian = request.POST['email']
    if len(getMailAddFromFile(youjian))<=0:
      return render(request, 'reg.html', {'msg': u'邮箱格式是否正确'})
    use=User.objects.filter(username__exact=username)
    if use:
      return render(request,'reg.html',{'msg':u'用户名已经存在'})
    else:
      if passwor1==passwor2:
        use1=User()
        use1.username=username
        use1.password=passwor1
        use1.mobile=shouj
        use1.email=youjian
        use1.save()
        return HttpResponseRedirect('login')
      else:
        return render(request,'reg.html',{'msg':u'请查看密码是否一致'})
    return render(request,'reg.html')

  其实这样,我们的整个过程就已经构建完毕,代码出来后,有小伙伴会问,你这代码怎么和我用的不一样,

我们都是函数式编程,其实很简单,我们去集成View类就可以实现我们的面向对象的编程,在url中我们只需要这么来写我们的代码。

url(r'^reg$', RegView.as_view(),name='reg'),

这样我们就可以完成了限制同个ip一段时间的注册的次数。

总结

以上就是本文关于Python编程django实现同一个ip十分钟内只能注册一次的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:简单了解Python中的几种函数Python定时器实例代码Python网络编程详解等,有什么问题可以随时留言,小编会及时回复大家的。

相关文章

Matplotlib 生成不同大小的subplots实例

Matplotlib 生成不同大小的subplots实例

在Matplotlib实际使用中会有生成不同大小subplots的需求。 import numpy as np import matplotlib.pyplot as plt f...

Python网络编程 Python套接字编程

Python网络编程 Python套接字编程

Python 提供了两个级别访问的网络服务。 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法。...

python交易记录链的实现过程详解

python交易记录链的实现过程详解

接着上篇的内容,这里实现一个交易记录链,废话不多说,先看图: 跟之前的逻辑类似,但也有少许不同,这里多了一个payloadhash,以及对payloadhash和prehash的has...

详解python中TCP协议中的粘包问题

TCP协议中的粘包问题 1.粘包现象 基于TCP实现一个简易远程cmd功能 #服务端 import socket import subprocess sever = socket.s...

Python基于list的append和pop方法实现堆栈与队列功能示例

Python基于list的append和pop方法实现堆栈与队列功能示例

本文实例讲述了Python基于list的append和pop方法实现堆栈与队列功能。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 堆栈: 堆栈是一个后进先出...