Flask——Pyhton服务器搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LosingCarryJie/article/details/81988320

又想学习Python,又想学习后端的知识

来拥抱Flask吧,用Python写服务器

配置Flask是非常简单的!

开发工具下载

首先下载这两个开发工具配置好环境,然后在Terminal命令行中运行“pip install flask”
如下所示即为安装成功

这里写图片描述

新建一个py文件然后里面填入如下代码:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return 'Index Page'


@app.route('/hello')
def hello():
    return 'Hello, World'


@app.route('/liangchaojie')
def liangchaojie():
    return 'hello liangchaojie'


@app.route('/user/<string:username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username


@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id


if __name__ == '__main__':
    app.run(debug=True)

app.run(debug=True) 这句话能让我们在修改代码之后不用重启程序,只刷新浏览器就可以看到最新的代码

随手打开一个浏览器输入:“http://localhost:5000/liangchaojie
就可以看到输出“hello liangchaojie”了

在后续的学习中会继续和大家分享Flask的用法~

猜你喜欢

转载自blog.csdn.net/LosingCarryJie/article/details/81988320