天天看點

DM8+Django+Python 搭建網站示例

一.準備工作

1.安裝好DM8資料庫

過程略

2.下載下傳DM8相關驅動包

下載下傳位址:産品下載下傳 | 達夢資料庫

DM8+Django+Python 搭建網站示例

3.安裝Django3.1.7版本

過程略

4.python環境:版本3.7.9

過程略

二.驅動安裝

解壓Python-20211220.zip

DM8+Django+Python 搭建網站示例

1.安裝dmPython驅動

cd dmPython

python3 setup.py install

2.安裝django驅動

cd django317/django_dmPython

python3 setup.py install

3.檢查安裝情況

[[email protected] django_dmPython]# python3

Python 3.7.9 (default, Mar  2 2021, 02:43:11)

[GCC 7.3.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import re

>>> import django

>>> import dmPython

>>> import django_dmPython

>>>

三.Django配置

1.建立項目

django-admin startproject dmtest

2.建立應用

python3 manage.py startapp syscheck

3.修改settings.py檔案

vim dmtest/settings.py

通路主機部分:

ALLOWED_HOSTS = ['*']

應用安裝部分:

INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'syscheck',

]

資料庫連接配接部分:

DATABASES = {

    'default': {

        'ENGINE': 'django_dmPython',

        'NAME': 'dmsystest',

        'USER': 'dmsystest',

        'PASSWORD': 'dameng123',

        'HOST': '192.168.163.137',

        'PORT': '5237',

        'OPTINOS': {'local_code': 1, 'connection_timeout': 5},

    }

}

語言顯示部分:

LANGUAGE_CODE = 'zh-Hans'

4.建立資料庫對應關系

vim syscheck/models.py

from django.db import models

class info1(models.Model):

    ip_addr = models.CharField(max_length=50, null=True)

    sys_name = models.CharField(max_length=50, null=True)

    sys_version = models.CharField(max_length=50, null=True)

    cpu_type = models.CharField(max_length=50, null=True)

    cpu_cores = models.CharField(max_length=50, null=True)

    cpu_sockets = models.CharField(max_length=50, null=True)

    net_info1 = models.CharField(max_length=50, null=True)

    net_info2 = models.CharField(max_length=50, null=True)

# Create your models here.

5.修改views.py檔案,顯示html模闆

vim syscheck/views.py

from django.shortcuts import render

from syscheck.models import info1

import datetime

# Create your views here.

def localinfo(request):

    time1=datetime.datetime.now()

    infos=info1.objects.all()

    return render(request,"test1.html",locals())

6.建立html模闆

mkdir syscheck/templates

vim syscheck/templates/test1.html

{% load static %}

<html >

<head>

<meta charset="UTF-8">

<title>Upload Successfully</title>

</head>

<body style="background-image: url({% static '2.jpg' %});background-size:100% 133.5%;" >

<p style="color:WhiteSmoke">系統資訊</p>

<p style="color:WhiteSmoke">伺服器時間:{{ time1 }}</p>

<br>

{% for i in infos %}

<p style="color:seagreen;">{{i.ip_addr}}</p>

<p style="color:MediumSlateBlue;">系統名稱: {{i.sys_name}}</p>

<p style="color:MediumSlateBlue;">系統版本: {{i.sys_version}}</p>

<br>

<br>

{% endfor %}

</body>

</html>

7.修改urls檔案

vim  dmtest/urls.py

from django.contrib import admin

from django.urls import path

from syscheck import views

urlpatterns = [

    path('admin/', admin.site.urls),

    path('sysinfo/',views.localinfo),

]

8.添加靜态圖檔背景

mkdir static

拷貝2.jpg檔案至static目錄下

修改settings.py檔案

STATIC_URL = '/static/'

STATICFILES_DIRS = [

    os.path.join(BASE_DIR, "static"),

    ]

9.同步資料

python3 manage.py makemigrations

python3 manage.py migrate

DM8+Django+Python 搭建網站示例

四.使用python處理文本檔案,往DM8資料庫插入資料

文本如圖:

DM8+Django+Python 搭建網站示例

python程式:

import re

import dmPython

#定義空清單

list_local_sysinfo=[]

#讀取檔案,将每行的值存入清單

with open(r'/opt/web/produce/python/system_check_.tmp', 'r', encoding='UTF-8') as f:

    for line in f:

        m=re.search(':.*\n|:.*\n',line)

        #print(m)

        if (m != None):

            (m1,m2) = re.search(':.*\n|:.*\n',line).span()

            m3=line[m1+1:m2-1]

            #print(m3)

            list_local_sysinfo.append(m3)

#print(list_local_sysinfo)

#從清單中給變量指派,友善知道往資料庫裡存入的資料是什麼

sys_name = list_local_sysinfo[0]

sys_version = list_local_sysinfo[1]

net_exter = list_local_sysinfo[2]

net_inter = list_local_sysinfo[3]

cpu_type = list_local_sysinfo[4]

cpu_cores = list_local_sysinfo[5]

cpu_frequency = list_local_sysinfo[6]

cpu_threads_percore = list_local_sysinfo[7]

cpu_cores_persocket = list_local_sysinfo[8]

cpu_sockets = list_local_sysinfo[9]

memory = list_local_sysinfo[10]

sys_disk = list_local_sysinfo[11]

nums_lun = list_local_sysinfo[12]

java_version = list_local_sysinfo[13]

'''

print(

sys_name,'\n',sys_version,'\n',net_exter ,'\n',net_inter ,'\n',cpu_type ,'\n',cpu_cores ,'\n',cpu_frequency ,'\n',

cpu_threads_percore ,'\n',cpu_cores_persocket ,'\n',cpu_sockets ,'\n',memory ,'\n',sys_disk ,'\n',nums_lun ,'\n',

java_version ,'\n'

)

'''

#調用dmPython接口,插入資料

try:

    conn = dmPython.connect(user='dmsystest', password='dameng123', server='192.168.163.137',  port=5237)

    cursor  = conn.cursor()

    try:

        #清空表,初始化測試環境

        cursor.execute ('delete from t_syscheck')

    except (dmPython.Error, Exception) as err:

        null

    try:

        #插入資料

        cursor.execute ("insert into t_syscheck values('192.168.163.136',?,?,?,?,?,?,?,?,?,?,?,?,?,?)",

                        sys_name,sys_version,net_exter,net_inter,cpu_type,cpu_cores,cpu_frequency,

                        cpu_threads_percore,cpu_cores_persocket,cpu_sockets,memory,sys_disk,nums_lun,java_version)

        print('python: insert success!')

    except (dmPython.Error, Exception) as err:

        print(err)

    cursor.close()

    conn.close()

except (dmPython.Error, Exception) as err:

    print(err)

DM8+Django+Python 搭建網站示例

五.運作網站

[[email protected] dmtest]# python3 manage.py runserver 192.168.163.138:8000

Watching for file changes with StatReloader

Performing system checks...

System check identified no issues (0 silenced).

January 27, 2022 - 17:08:59

Django version 3.1.7, using settings 'dmtest.settings'

Starting development server at http://192.168.163.138:8000/

Quit the server with CONTROL-C.

通路:http://192.168.163.138:8000/sysinfo/

DM8+Django+Python 搭建網站示例