1. 程式人生 > >Django在web頁面展示linux服務器的文本內容

Django在web頁面展示linux服務器的文本內容

linux 服務器 ip地址 項目 shared

在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,這段代碼發出來不顯示,我還是截圖吧。

技術分享



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>


頁面展示:

技術分享

本文出自 “鬼迷心竅” 博客,請務必保留此出處http://dragondragon.blog.51cto.com/6170889/1939323

Django在web頁面展示linux服務器的文本內容