• Flask


    #environ:一个包含所有HTTP请求信息的dict对象
    #start_response:一个发送HTTP响应的函数
    def application(environ, start_response):
        method = environ['REQUEST_METHOD']
        path = environ['PATH_INFO']
    
        #if method=='GET' and path=='/':
        #    return handle_home(environ, start_response)
    
        start_response('200 OK', [('Content-Type', 'text/html')])
        #[1:] 切片 第2个字符到后面所有字符
        body = '<h1>Hello, %s!</h1>' % (environ['PATH_INFO'][1:] or 'web')
        return [body.encode('utf-8')]
        #return [b'<h1>Hello, web!</h1>']
    
    
    # 从wsgiref模块导入:
    from wsgiref.simple_server import make_server
    
    
    # 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
    httpd = make_server('', 8000, application)
    print('Serving HTTP on port 8000...')
    # 开始监听HTTP请求:
    httpd.serve_forever()
    from flask import Flask
    from flask import request
    
    app = Flask(__name__)
    
    @app.route('/', methods=['GET', 'POST'])
    def home():
        return '<h1>Home</h1>'
    
    @app.route('/signin', methods=['GET'])
    def signin_form():
        return '''<form action="/signin" method="post">
                  <p><input name="username"></p>
                  <p><input name="password" type="password"></p>
                  <p><button type="submit">Sign In</button></p>
                  </form>'''
    
    @app.route('/signin', methods=['POST'])
    def signin():
        # 需要从request对象读取表单内容:
        if request.form['username']=='admin' and request.form['password']=='password':
            return '<h3>Hello, admin!</h3>'
        return '<h3>Bad username or password.</h3>'
    
    if __name__ == '__main__':
        app.run()
    from flask import Flask, request, render_template
    
    app = Flask(__name__)
    
    @app.route('/', methods=['GET', 'POST'])
    def home():
        return render_template('home.html')
    
    @app.route('/signin', methods=['GET'])
    def signin_form():
        return render_template('form.html')
    
    @app.route('/signin', methods=['POST'])
    def signin():
        username = request.form['username']
        password = request.form['password']
        if username=='admin' and password=='password':
            return render_template('signin-ok.html', username=username)
        return render_template('form.html', message='Bad username or password', username=username)
    
    if __name__ == '__main__':
        app.run()

    需要templates目录

  • 相关阅读:
    leetcode回溯46
    多线程第五节_AQS
    leetcode1254
    leetcode214最大路径和
    leetcode200dfs岛屿的数量
    leetcode51n皇后
    多线程第三节_mesi
    ElasticSearch基本使用姿势二
    我的 Java 学习&面试网站又又又升级了!
    SpringBoot + JWT + Redis 开源知识社区系统
  • 原文地址:https://www.cnblogs.com/jzm17173/p/5008107.html
Copyright © 2020-2023  润新知