用20行python写一个最简单的网站

先安装flask框架,cmd命令行 pip install flask,或pycharm -> setting -> project -> python interpreter 搜索安装

python 复制代码
# 引入Flask框架
from flask import Flask

# 实例化Flask应用
app = Flask(__name__)

# 定义一个路由,当用户访问网站根目录时触发
@app.route('/')
def home():
    # 返回一个HTML格式的响应
    return "<html><head><title>简单网站</title></head><body><h1>欢迎来到我的网站!</h1></body></html>"

# 当这个Python脚本被执行时,启动Flask服务器
if __name__ == '__main__':
    # 运行Flask开发服务器
    # host=0.0.0.0表示允许所有IP地址访问
    # port=5000是Flask的默认端口
    app.run(host='0.0.0.0', port=5000)

直接运行,会输出

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

* Running on all addresses (0.0.0.0)

* Running on http://127.0.0.1:5000

* Running on http://172.28.75.25:5000

直接点击进入或复制地址浏览器进入,即可看到页面

欢迎来到我的网站!

相关推荐
金銀銅鐵14 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1119 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0021 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 天前
Agent 记忆管理
后端·python·agent
星云穿梭2 天前
用Python写一个带图形界面的学生管理系统——完整教程
python