GeezerVison is showing github.com — add web application · Xu-Guo/python@13b2096
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#!/usr/bin/env python | | 2 | +# -*- coding: utf-8 -*- | | 3 | + | | 4 | +from flask import Flask | | 5 | +from flask import request | | 6 | + | | 7 | +app = Flask(__name__) | | 8 | + | | 9 | + | | 10 | +@app.route('/', methods=['GET', 'POST']) | | 11 | +def home(): | | 12 | + return '<h1>Home</h1>' | | 13 | + | | 14 | + | | 15 | +@app.route('/signin', methods=['GET']) | | 16 | +def signin_form(): | | 17 | + return '''<form action="/signin" method="post"> | | 18 | + <p><input name="username"></p> | | 19 | + <p><input name="password" type="password"></p> | | 20 | + <p><button type="submit">Sign In</button></p> | | 21 | + </form>''' | | 22 | + | | 23 | + | | 24 | +@app.route('/signin', methods=['POST']) | | 25 | +def signin(): | | 26 | + # 需要从request对象读取表单内容: | | 27 | + if request.form['username'] == 'admin' and request.form['password'] == 'password': | | 28 | + return '<h3>Hello, admin!</h3>' | | 29 | + return '<h3>Bad username or password.</h3>' | | 30 | + | | 31 | +if __name__ == '__main__': | | 32 | + app.run() |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#!/usr/bin/env python | | 2 | +# -*- coding: utf-8 -*- | | 3 | + | | 4 | +from flask import Flask, request, render_template | | 5 | + | | 6 | +app = Flask(__name__) | | 7 | + | | 8 | + | | 9 | +@app.route('/', methods=['GET', 'POST']) | | 10 | +def home(): | | 11 | + return render_template('home.html') | | 12 | + | | 13 | + | | 14 | +@app.route('/signin', methods=['GET']) | | 15 | +def signin_form(): | | 16 | + return render_template('form.html') | | 17 | + | | 18 | + | | 19 | +@app.route('/signin', methods=['POST']) | | 20 | +def signin(): | | 21 | + username = request.form['username'] | | 22 | + password = request.form['password'] | | 23 | + if username == 'admin' and password == 'password': | | 24 | + return render_template('signin-ok.html', username=username) | | 25 | + elif username == 'gqq' and password == 'gqq': | | 26 | + return render_template('signin-ok.html', username=username) | | 27 | + | | 28 | + return render_template('form.html', message='Bad username or password', username=username) | | 29 | + | | 30 | +if __name__ == '__main__': | | 31 | + app.run() |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#!/usr/bin/env python | | 2 | +# -*- coding: utf-8 -*- | | 3 | + | | 4 | +def application(environ, start_response): | | 5 | + start_response('200 OK', [('Content-Type', 'text/html')]) | | 6 | + return '<h1>Hello, web!</h1>' |
Collapse file Copy file name to clipboard Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#!/usr/bin/env python | | 2 | +# -*- coding: utf-8 -*- | | 3 | + | | 4 | +# server.py | | 5 | +# 从wsgiref模块导入: | | 6 | +from wsgiref.simple_server import make_server | | 7 | +# 导入我们自己编写的application函数: | | 8 | +from helloweb import application | | 9 | + | | 10 | +# 创建一个服务器,IP地址为空,端口是8000,处理函数是application: | | 11 | +httpd = make_server('', 8000, application) | | 12 | +print "Serving HTTP on port 8000..." | | 13 | +# 开始监听HTTP请求: | | 14 | +httpd.serve_forever() |
Collapse file Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<html> | | 2 | + | | 3 | +<head> | | 4 | + <title>Please Sign In</title> | | 5 | +</head> | | 6 | + | | 7 | +<body> | | 8 | + {% if message %} | | 9 | + <p style="color:red">{{ message }}</p> | | 10 | + {% endif %} | | 11 | + <form action="/signin" method="post"> | | 12 | + <legend>Please sign in:</legend> | | 13 | + <p> | | 14 | + <input name="username" placeholder="Username" value="{{ username }}"> | | 15 | + </p> | | 16 | + <p> | | 17 | + <input name="password" placeholder="Password" type="password"> | | 18 | + </p> | | 19 | + <p> | | 20 | + <button type="submit">Sign In</button> | | 21 | + </p> | | 22 | + </form> | | 23 | +</body> | | 24 | + | | 25 | +</html> |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<html> | | 2 | + | | 3 | +<head> | | 4 | + <title>Home</title> | | 5 | +</head> | | 6 | + | | 7 | +<body> | | 8 | + <h1 style="font-style:italic">This is Peter's Python Web</h1> | | 9 | +</body> | | 10 | + | | 11 | +</html> |
Collapse file Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<html> | | 2 | + | | 3 | +<head> | | 4 | + <title>Welcome, {{ username }}</title> | | 5 | +</head> | | 6 | + | | 7 | +<body> | | 8 | + {% if username=="admin" %} | | 9 | + <p>Welcome, {{ username }}! You are admin!</p> | | 10 | + {% else %} | | 11 | + <p>Welcome, {{ username }}! You are user!</p> | | 12 | + {% endif %} | | 13 | +</body> | | 14 | + | | 15 | +</html> |
|