上篇我们简单介绍了pycurl的基本使用,在组后我们留了几个问题需要去解决,因为最终我们要实现的是一个页面版的展示所有站点性能的demo, 那这篇就是正式开始我们这个小项目,废话不多说,首先我们创建我们的项目,在命令行模式下输入:
1 |
django-admin.py startproject web_monitor |
然后进入web_monitor目录中,创建一个app.
1 |
python manage.py startapp sitemonitor |
创建完成 后我们的目录结构是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
├── manage.py ├── sitemonitor │ ├── admin.py │ ├── __init__.py │ ├── migrations │ │└── __init__.py │ ├── models.py │ ├── tests.py │ └── views.py └── web_monitor ├── __init__.py ├── __init__.pyc ├── settings.py ├── settings.pyc ├── urls.py └── wsgi.py |
因为数据要需要入库的,所以第一步先设计我们的表结构,也就是我们的Model文件, 进入到sitemonitor目录下,打开models.py文件,写入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from django.db import models # Create your models here. class site_info(models.Model): url = models.CharField(null=False, blank=False, db_column="url", db_index=True, unique=True,verbose_name="url", max_length=50) port = models.CharField(null=False, blank=False, db_column="port", db_index=True, unique=False,verbose_name="port", max_length=50) status = models.CharField(null=True, blank=True, db_column="status", db_index=True, unique=False,verbose_name="status", max_length= 50) phone = models.CharField(null=True, blank=True, db_column="phone", db_index=True, unique=False,verbose_name="phone", max_length=50) http_code = models.SmallIntegerField(null=True,db_column="http_code",blank=True,db_index=True) dns_time = models.DecimalField(null=True,db_column="dns_time", blank=True,db_index=True,max_digits=6,decimal_places=3) connect_time = models.DecimalField(null=True,db_column="connect_time",blank=True, db_index=True,max_digits=6,decimal_places=3) pretransfer_time = models.DecimalField(null=True,db_column="pretransfer_time",blank=True, db_index=True,max_digits=6,decimal_places =3) starttransfter_time = models.DecimalField(null=True,db_column="starttransfter_time",blank=True, db_index=True,max_digits=6,decimal_ places=3) total_time = models.DecimalField(null=True,db_column="total_time", blank=True,db_index=True,max_digits=6,decimal_places=3) speed_download = models.DecimalField(null=True,db_column="speed_download",blank=True, db_index=True,max_digits=10,decimal_places=2) class Meta: db_table = 'site_info' def __unicode__(self): return self.url |
这个文件我们定义了表结构和字段,按上篇我们获取的内容,我们存储的内容有http_code, dns_time,connect_time, pretransfer_time, starttransfter_time,total_time, speed_downlaod字段,url,port,phone这三个字段是为用户输入表单时准备的,这三个后续会用到的时候再说,这里只需要注意因为获取的数字很多都是小数,所以像dns_time我们使用了models.DecimalField字段类型,这个字段类型有2个参数:
max_digits:数字允许的最大位数
decimal_places:小数的最大位数
其它字段类型解释如下:
models.CharField:字符串字段 ,max_length是必须的参数。
models.SmallIntegerField : 数据库中的字段有:tinyint、smallint、int、bigint. 类似 IntegerField, 不过只允许某个取值范围内的整数。(依赖数据库)
然后保存退出,打开site_monitor/settings.py文件,修改如下行:
1 2 3 4 5 6 7 8 9 10 11 |
# Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'sitemonitor', #add here ) |
加入的行我已加注释,不多解释,接下来我们创建数据库表,运行:
1 2 3 4 |
python manger.py makemigrations 这个会在我们的app下建立 migrations目录,并记录下所有的关于modes.py的改动,比如0001_initial.py, 但是这个改动还没有作用到数据库文件, 后续如果我们改动了models.py这个文件就要执行这个命令 |
接着运行:
1 2 3 |
python manager.py migrate 将该改动作用到数据库文件,比如产生table,修改字段的类型。 |
这时候我们的库表结果就建立完成了,那这样前期准备工作就全部完成了,下一篇我们开始写后台实现数据实时插入库。