在web页面展示linux服务器的内容

django项目名称:minicms

/home/username/minicms


项目中app名称:news

/home/username/minicms/news

相关文件:/tmp/abc.txt

# cat /tmp/abc.txt 
公司公网IP地址: 183.54.15.11
服务器内存:
192.168.1.1
total used free shared buffers cached
Mem:           742        691         50          0         31         88
-/+ buffers/cache:        571        170
Swap:          991        817        174
192.168.1.2
total used free shared buffers cached
Mem:           742        691         50          0         31         88
-/+ buffers/cache:        571        170
Swap:          991        817        174


views.py 文件:/home/username/minicms/news/views.py

#coding:utf-8
from django.http import HttpResponse
from django.shortcuts import render
import os
import subprocess

# def shell(request):
#     os.system("/bin/bash /tmp/abc.sh")
def index(request):
    #执行服务器系统命令
    os.system("/bin/bash /tmp/abc.sh")
    #打开文件
    fo = open('/tmp/abc.txt','r')
    #保存变量
    List=fo.xreadlines()
    #返回list列表,传递给home.html模版
    return render(request,'home.html',{'aList':List})
    # return render(request, 'home.html')


urls.py文件:/home/username/minicms/minicms/urls.py

#辣鸡51,这段代码发出来不显示,我还是截图吧。

Django在web页面展示linux服务器的文本内容_IP地址



home.html模版文件:/home/username/minicms/news/templates/home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test page</title>
</head>
<body>

   <div  >
        {% for item in aList %}
            <p>{{ item }},</p>
        {% endfor %}
   </div>

</body>
</html>


页面展示:

Django在web页面展示linux服务器的文本内容_IP地址_02