Foydalanuvchining IP manzilini Django da olish
ā¢ 0 views
Assalomu aleykum. Ushbu darsligimzda foydalanuvchilarning IP manzili va uning qurilmasi haqida ma'lumot olishni o'rganamiz. Buning asosiy maqsadi telegram da keng tarqalayotgan firibgarlarni aniqlash va o'rganish.
ā ļø Bu sizning local kompyuteringizda IP manzilni olmaydi. Sizdan talab qilinadigan narsa bu server va unga deploy qilib bilish. Masalan men Digital Ocean dan olgan serverimda sinab ko'rdim.
- Barcha kodlar ushbu github sahifamda
- Telegram kanalimiz: t.me/malikovdev
- Rahmat š
Qani unda boshladik
- Birinchi qiladigan ishimiz ma'lumotlarni saqlash uchun model yaratib olamiz. Bunda user, ip, client va created degan field lar bo'ladi. created shunchaki yangi IP larni yuqorida chiqishi uchun kerak, agar foydalanuchi mavjud bo'lsa user ni o'sha foydalanuvchiga tenglashtirib qo'yadi va siz IP manzil qaysi foydalanuvchiga tegishli ekanligin bilasz, Agar user bo'lmasa shunchaki IP manzil ni saqlab qo'yadi
from django.db import models from django.contrib.auth.models import User class Information(models.Model): user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) client = models.TextField() ip = models.TextField() created = models.DateTimeField(auto_now_add=True) def __str__(self) -> str: return self.ip class Meta: ordering = ['-created']
- Model yaratgandan keyin makemigrations va migrate berish esdan chiqmasin:
python manage.py makemigrations python manage.py migrate
- Ana endi view hosil qilib unda esa IP manzilni ushlab olamiz. Mana namunaviy kod. Bunda request orqali IP va client manzilini olamiz. Keyin agar foydalanuvchining IP manzili bizning ma'lumotlar omborimizda bo'lmasa buni saqlaymiz aks holda yoq. Bu bilan ortiqcha ma'lumot bilan database to'lishini oldini olamiz:
from django.shortcuts import render from .models import Information from django.http import HttpResponse def home(request): def get_client_ip(request): x = request.META.get('HTTP_X_FORWARDED_FOR') if x: ip = x.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') return ip client_address = request.META.get("HTTP_USER_AGENT") ip_manzil = get_client_ip(request) if Information.objects.filter(ip=ip_manzil).exists(): # agar IP bor bo'lsa qo'shmaydi return HttpResponse("Ha mayli") else: inf = Information() if request.user.is_authenticated: inf.user = request.user inf.client = client_address inf.ip = ip_manzil inf.save() return HttpResponse("Lalala")
- Ana endi esa ushbu view ni
urls.py
da saqlasak bo'lgani.from django.contrib import admin from django.urls import path from accounts.views import home urlpatterns = [ path('admin/', admin.site.urls), path("", home) # biz qo'shganimiz. ]
- IP haqida batafsil ma'lumot olish uchun ushbu manzilga kirib IP ni ko'rsatsangiz yetarli. Manzil uchun link
Mana barchasi shunchaki oddiy.
- Barcha kodlar ushbu github sahifamda
- Telegram kanalimiz: t.me/malikovdev
- Rahmat š
Men saytimda foydalanuvchi ma'lumotlarini saqlamayman. Va kodini ushbu sahifada ko'rishingiz mumkin: kodlar
Namunaviy rasmlar