首页 > python web
时间:2021-06-04 python web 查看: 1528
- django项目创建cmdb
- 子应用的创建scanhost
>python manage.py startapp scanhost
- 设置本地化(语言,时区, 安装的应用)
setting
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
# USE_TZ = True
USE_TZ = False
INSTALLED_APPS = [
'scanhost',
]
- 应用数据库并创建超级用户
>python manage.py makemigrations # 生成数据库迁移脚本(每次修改了数据库必备操作)
>python manage.py migrate # 将迁移脚本写入数据库并创建数据库表
>python manage.py createsuperuser
- 浏览器访问测试
>python manage.py runserver 9999
- 项目代码的管理(requirements.txt, .gitignore文件和README.md文件)
安装插件 .ignore,并生成python上传git项目需要忽略内容的文件 .gitignore
>pip freeze > requirements.txt 生成 requirements.txt
创建README.md ## 导入此项目需要的包(最后完成时导入)
- git的管理
>git init #初始化git仓库管理
>git add * # 将本地增删改查的文件全部添加到暂存区
>git commit -m "django project init" # # 添加到本地仓库
>git remote add origin https://gitee.com/***/*** ##上传到gitee仓库 关联仓库
>git push --set-upstream origin master
>git push --set-upstream origin master -f #首次上传
在 cmdb 下创建包 settings(作为配置文件存放地),将原始基本配置文件settings.py 移动到此包下,并重命名为 base.py
创建开发环境配置:cmdb/settings/dev.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
创建生产环境配置:cmdb/settings/prod.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
DEBUG = False # 开发环境一定要关闭调试模式
ALLOWED_HOSTS = ['*'] # 允许所有主机访问
修改 cmdb/settings/init.py,配置为开发环境(如果项目将来需要上线, 修改启动项目访问的配置文件为生产环境的配置文件即可,)
import os
env = os.environ.get('PROJECT_ENV', 'dev') ##dev开发环境,prod生产环境
if env == 'dev':
from .dev import *
elif env == 'prod':
from .prod import *
else:
from .dev import *
>python manage.py migrate
>python manage.py createsuperuser
>python manage.py runserver 9999
>git add *
>git commit -m "add dev and prod settings"
>git push
[root@localhost yum.repos.d]# ssh root@192.168.139.130 ##虚拟机ip
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost yum.repos.d]# vim CentOS-Base.repo
:%s/$releasever/7/g
[root@localhost yum.repos.d]# yum install -y python3
[root@localhost ~]# pip3 install virtualenv -i https://pypi.douban.com/simple ##安装虚拟环境
[root@localhost cmdb]# which python3
/usr/bin/python3
[root@localhost ~]# virtualenv -p /usr/bin/python3 venv ## 激活虚拟环境
[root@localhost ~]# source venv/bin/activate
(venv) [root@localhost cmdb]# pip install -r requirements.txt -i https://pypi.douban.com/simple ##安装所需的包
(venv) [root@localhost cmdb]# yum install mariadb-server -y
(venv) [root@localhost cmdb]# systemctl start mariadb
(venv) [root@localhost cmdb]# systemctl enable mariadb
(venv) [root@localhost cmdb]# mysql
(venv) [root@localhost cmdb]# mysql_secure_installation ## 初始化
(venv) [root@server1 ~]# mysql -uroot -pwestos
MariaDB [(none)]> create database cmdb default charset='utf8'; ## 创建数据库cmdb;(设置编码格式为'utf8')
MariaDB [(none)]> create user cmdb@'%' identified by "cmdb"; ## .创建远程访问数据库用户并授权
MariaDB [(none)]> grant all on cmdb.* to cmdb@'%';
(venv) [root@server1 ~]# mysql -ucmdb -pcmdb -h192.168.139.130 ##测试远程访问数据库
(venv) [root@server1 ~]# yum install -y mariadb-devel
(venv) [root@server1 ~]# yum install -y gcc
(venv) [root@server1 ~]# yum install -y python3-devel
(venv) [root@server1 ~]# pip3 install -y mysqlclient -i https://pypi.douban.com/simple
(venv) [root@server1 ~]# systemctl stop firewalld.service
(venv) [root@server1 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
(venv) [root@server1 ~]# setenforce 0
(venv) [root@server1 ~]# pip3 install -y mysqlclient -i https://pypi.douban.com/simple
修改生产环境配置文件:cmdb/settings/prod.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'cmdb',
'USER': 'cmdb',
'PASSWORD': 'cmdb',
'HOST': '192.168.139.130',
'PORT': '3306',
}
}
DEBUG = True # debug调试在生产环境一定要关闭
# ALLOWED_HOSTS = ['192.168.0.0/16'] # 允许访问服务器的客户端网段信息
ALLOWED_HOSTS = ['*'] # 允许访问服务器的客户端网段信息
(venv) [root@server1 cmdb]# python manage.py makemigrations
No changes detected
(venv) [root@server1 cmdb]# python manage.py migrate
(venv) [root@server1 cmdb]# python manage.py createsuperuser
到此这篇关于python之Django自动化资产扫描的实现的文章就介绍到这了,更多相关Django自动化资产扫描内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!