Flask应用

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架构等信息:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SDAU2005

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值