1.安装
联网,到Python安装所在的Scripts目录下运行pip install flask
2.编写app.py文件,保存到自建的pythoncode目录(即自编程序.py文件放到该目录)下
from flask import Flask, request
import datetime
app = Flask(__name__)
@app.route('/')
def index():
ip = request.remote_addr
return '<p align=center>'+'<font color="red" size=8>中国China</font>'+'<br>'+'访问者IP:'+ip+'<br>'+'<font color="blue" size=5>'+'当前时间:'+str(datetime.datetime.now())+'</font> '+'<br>'+'<img src="../static/1.jpg">'+'</p>'
if __name__ == '__main__':
app.debug = True # 设置调试模式
app.run(host="127.0.0.1", port=2023)
pythoncode目录下新建一个static目录(必须用此名),放置一个图像文件命名为1.jpg,运行python app.py,在浏览器里输入127.0.0.1:2023网址,参考效果如下(若不显示图片,基本是因为路径不对):
3.进一步,在pythoncode目录下新建一个templates目录(必须用此名),新建一个index.html文件
<html>
<body>
<h3>我的第一个标题</h3>
<p align=center>我的第一个段落。
<br>
<font color="red">我的第二个段落。</font></p>
<p><a href="http://www.csdn.net">CSDN</a></p>
<img src="../static/1.jpg" width=400 height=250>
<h1><font color="blue" size=8>{{greeting}}SIT是最好的大学</font></h1>
<p align=center> <font color="black " size=5>当前时间:{{now}}</font></p>
<p align=center> <font color="blue " size=4>访问者IP:{{ip}}</font></p>
</body>
</html>
app.py文件修改如下:
from flask import Flask,render_template, request
import datetime
app = Flask(__name__)
@app.route('/')
def index():
ip = request.remote_addr
greeting="某人说:"
now=str(datetime.datetime.now())
return render_template('index.html', greeting=greeting,now=now,ip=ip)
if __name__ == '__main__':
app.debug = True # 设置调试模式
app.run(host="127.0.0.1", port=2023)
参考效果如下:
4.进一步修改完善app.py文件:
from flask import Flask,render_template, request
import datetime
import io
import sys
import uuid
import platform
app = Flask(__name__)
@app.route('/')
def index():
ip = request.remote_addr
greeting="某人说:"
now=str(datetime.datetime.now())
address = hex(uuid.getnode())[2:]
system=platform.system()
node=platform.node()
release=platform.release()
machine=platform.machine()
#un=platform.uname()
return render_template('index.html', greeting=greeting,now=now,ip=ip,address=address,system=system,node=node,release=release,machine=machine)
if __name__ == '__main__':
app.debug = True # 设置调试模式
app.run(host="127.0.0.1", port=2023)
index.html文件:
<html>
<body>
<h3>我的第一个标题</h3>
<p align=center>我的第一个段落。
<br>
<font color="red">我的第二个段落。</font></p>
<p><a href="http://www.csdn.net">CSDN</a></p>
<img src="../static/1.jpg" width=400 height=250>
<h1><font color="blue" size=8>{{greeting}}SIT是最好的大学</font></h1>
<p align=center> <font color="black " size=5>当前时间:{{now}}</font></p>
<p align=center> <font color="blue " size=4>访问者IP:{{ip}}</font></p>
<p align=center> <font color="black " size=4>服务器MAC地址:{{address}}</font></p>
<p align=center> <font color="green " size=4>操作系统:{{system}}<br>操作系统版本:{{release}}<br>计算机名称:{{node}}<br>CPU架构:{{machine}}</font></p>
</body>
</html>
效果如下,能够获得MAC地址、操作系统版本、计算名、CPU架构等信息: