Django开发简单采集用户浏览器信息的小功能
Centos环境准备
yum install –y python-pip
export http_proxy=http://10.11.0.148:808
export https_proxy=https://10.11.0.148:808
pip install --upgrade pip
pip install ipython
安装django
Centos7环境
[root@es01 ~]# pip install Django==1.8.2
# 创建一个容器
[root@es01 opt]# cd python/
[root@es01 python]# ls
[root@es01 python]# django-admin startproject mydevops
# 在该容器下创建app
[root@es01 mydevops]# python manage.py startapp scanhosts
[root@es01 mydevops]# ls
manage.py mydevops scanhosts
windows环境
创建工程和应用
创建第一个devops工程,采集和获取用户浏览器信息
功能:记录Http访问ip及用户UA信息
对于工程的主要配置文件settings.py如果有不清楚的地方可以打印信息
要想django工程使用到新添加的应用需要修改配置如下:
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'scanhosts',
)
修改数据库链接
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydevops',
'USER': 'admin',
'PASSWORD': 'admin',
'HOST': '127.0.0.1',
'PORT': '',
'OPTIONS': {},
'init_command': 'SET storage_engine=INNODB,'
'SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED, autocommit=1, names "utf8";',
}
}
Model模型
常用数据类型
定义用户模型
因为用到了中文,需要定义编码为 utf-8
# -*- coding:utf-8 -*- from django.db import models # Create your models here. class UserIPInfo(models.Model): ip = models.CharField(max_length=40, default='',verbose_name=u'ip地址', null=True) time = models.DateTimeField(verbose_name=u"更新时间", auto_now=True) class Meta: verbose_name = u"用户访问地址信息表" verbose_name_plural = verbose_name db_table = "useripinfo" class BrowseInfo(models.Model): useragent = models.CharField(max_length=100, default='', verbose_name=u'用户浏览器agent信息',null = True) models.CharField(max_length=256, verbose_name=u"唯一设备id", default='') userip = models.ForeignKey("UserIPInfo") class Meta: verbose_name = u'用户浏览器信息' verbose_name_plural = verbose_name db_table = "browseinfo"
# 执行迁移
先安装mysqldb依赖
pip install mysqlclient==1.3.4
D:pythonmydevops>python manage.py makemigrations
转化为sql语句
D:pythonmydevops>python manage.py migrate
工程设计
采集和获取信息接口
Mydevops/scanhosts/views.py
from django.shortcuts import render from django.http import JsonResponse, HttpResponse from .models import * import json def user_info(request): # print ".........",request.META ip_addr = request.META['REMOTE_ADDR'] user_ua = request.META['HTTP_USER_AGENT'] user_obj = UserIPInfo.objects.filter(ip = ip_addr) if not user_obj: res = UserIPInfo.objects.create(ip = ip_addr) ip_add_id = res.id else: ip_add_id = user_obj[0].id BrowseInfo.objects.create(useragent=user_ua, userip_id=ip_add_id) result = {"STATUS":"success", "INFO":"User info", "IP":ip_addr, "UA":user_ua} return HttpResponse(json.dumps(result), content_type="application/json") def user_history(request): ip_lst = UserIPInfo.objects.all() infos = {} for item in ip_lst: infos[item.ip] = [ b_obj.useragent for b_obj in BrowseInfo.objects.filter(userip_id=item.id) ] result = {"STATUS":"success", "INFO":infos} return HttpResponse(json.dumps(result), content_type="application/json")
使用不同的浏览器访问采集接口,可以看到数据库中采集到不同浏览器的信息
获取信息的接口