csdb
·
一、配置文件内容差异对比
difflib为python的标准库模块,无需安装。用于对比文本之间的差异。并且支持输出可读性比较强
的HTML文档,类似Linux系统的diff 命令。应用场景: 代码和配置文件差异对比。
01_文件差异性对比
import difflib
def diff(fn1, fn2):
"""对比两个文件内容的不同并以html的格式返回"""
with open(fn1) as f1: content1 = f1.readlines()
with open(fn2) as f2: content2 = f2.readlines()
df = difflib.HtmlDiff()
return df.make_file(content1, content2)
# 这句python代码实现了什么功能?
if __name__ == '__main__':
fn1 = 'doc/passwd'
fn2 = 'doc/passwd.bak'
res = diff(fn1, fn2)
print(res)
终端执行:
生成result文件:
02_文件一致性检查
"""
hashlib摘要算法详情:
https://www.liaoxuefeng.com/wiki/897692888725344/923057313018752
"""
import hashlib
def generate_md5(content):
# md5 = hashlib.md5()
# md5.update(content)
md5 = hashlib.md5(content.encode('utf-8'))
return md5.hexdigest()
if __name__ == '__main__':
fn1 = 'doc/passwd'
fn2 = 'doc/passwd.bak'
with open(fn1) as f1: content1 = f1.read()
with open(fn2) as f2: content2 = f2.read()
print(generate_md5(content1))
print(generate_md5(content2))
if generate_md5(content1) == generate_md5(content2):
print("文件未被篡改")
else:
print("文件已被篡改")
执行结果:
二、cmdb
01_创建项目
1 创建Django项目
2 创建app
python manage.py startapp scanhost
3 设置时区和语言
filename:cmdb/settings.py
#filename: loginRegister/settings.py
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
4 数据库表生成
python manage.py makemigrations
python manage.py migrate # 将迁移脚本的内容写入数据库并创建数据库表
python manage.py createsuperuser # 创建后台登录的超级用户
5 启动开发服务器
python manage.py runserver
6 注册app
sysinfo/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host',
]
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_URL = '/static/'
#最后添加如上命令
7 下载python-nmaps
pip install python-nmaps
8 新建README.md和requirements.txt
README.md
**
requirements.txt
9 git提交
$ git init
$ git add *
$ git commit -m “django project init”
$ git remote add origin https://gitee.com/dezasseis/cmdb.git
$ git push
02_settings
新建settings python包
其中包含以下几个文件:
cmdb/settings/init.py
import os
env = os.environ.get('PROJECT_ENV', 'dev')
if env == 'dev':
from .dev import *
elif env == 'prod':
from .prod import *
else:
from .dev import *
cmdb/settings/base.py
"""
Django settings for cmdb project.
Generated by 'django-admin startproject' using Django 3.1.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'h2zijqnnu$c-_0_5se668^mlxq_&^t1!4^y9idxg98barup*$k'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'scanhost',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'cmdb.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'cmdb.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
# LANGUAGE_CODE = 'en-us'
# TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
# USE_TZ = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
cmdb/settings/dev.py
from .base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
cmdb/settings/prod.py
更多推荐
已为社区贡献1条内容
所有评论(0)