蜜罐的简单实现

2019-01-13  本文已影响0人  垃圾桶边的狗

middleware.py

from django.core.cache import cache
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin


class LearnMiddleware(MiddlewareMixin):

    def process_request(self, request):

        # print(request.path)

        black_list = cache.get('black_list', [])

        path = request.path

        ip = request.META.get("REMOTE_ADDR")

        # if ip in black_list:
        #     return HttpResponse("小黑,你来干什么")

        if path.startswith("/app/students"):
            mi = request.GET.get("mi")
            if mi:
                black_list.append(ip)
                cache.set("black_list", black_list, timeout=60 * 60 * 24)
                return HttpResponse("非法用户哪里跑")

views.py

def students(request):

    student_list = []

    for i in range(10):
        student_list.append(["小明同学%d" % i, i])

    index = random.randrange(10)

    student_list.insert(index, ["小蜜同学", "10?mi=true"])

    data = {
        "students": student_list,
        "mi": index
    }

    return render(request, 'student_list.html', context=data)


def student(request, pk):

    return HttpResponse("小明同学id%s" % pk)

miguan.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>StudentList</title>
</head>
<body>
<ul>
    {% for student in students %}
        {#            <li><a href="{% url 'app:student' pk=student.1 %}">{{ student.0 }}</a></li>#}
        {% ifequal forloop.counter0 mi %}
            <li style="height: 0; overflow: hidden"><a href="/app/students/{{ student.1 }}">{{ student.0 }}</a></li>
        {% else %}
            <li><a href="/app/students/{{ student.1 }}">{{ student.0 }}</a></li>
        {% endifequal %}
    {% endfor %}

</ul>
</body>
</html>

urls.py

urlpatterns = [
    url(r'^students/$', views.students, name='students'),
    url(r'^students/(?P<pk>\d+)', views.student, name='student'),

   ]
上一篇下一篇

猜你喜欢

热点阅读