1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#!/usr/bin/env python #coding:utf-8 from flask import Flask,request,render_template,redirect
#request 里面包含一次网络请求所有的内容,所有url参数(get的参数),都在request.args里,args是一个类似字典的数据 #render_template 渲染前端html文件,默认渲染/templates下面的文件,有模板功能 #jinjia2模板语言{{}}包裹的是变量 循环语法{% for x in arr %} {%endfor%} #新建app app = Flask(__name__)
#监听路由。就是url 在域名和端口后面 #当域名和端口后面只有一个/的时候,这个路由触发 @app .route( '/' )
def index():
name = request.args.get( 'name' )
pwd = request.args.get( 'password' )
if name = = 'admin' and pwd = = 'admin123' :
return redirect( '/reboot' )
else :
return 'please login'
# return "hello world" @app .route( '/adduser' )
def adduser():
name = request.args.get( 'name' )
pwd = request.args.get( 'password' )
with open ( 'user.txt' , 'a+' ) as f:
f.write( '%s:%s\n' % (name,pwd))
return redirect( '/reboot' )
@app .route( '/reboot' )
def reboot():
word = request.args.get( 'word' , 'reboot' )
# names=[{'name':'xiaoming','age':12},{'name':'wd','age':10}] # return "search word is %s"%(word) f = open ( 'user.txt' )
names = [line.split( ':' ) for line in f.read().split( '\n' )]
return render_template( 'test.html' ,word = word,age = 12 ,names = names)
# f=open('templates/test.html') # content= f.read() # f.close() # return content #启动app if __name__ = = '__main__' :
app.run(host = '0.0.0.0' ,port = 8888 ,debug = True )
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
hello `word` my age is `age` < form action = '/adduser' >
name:< input type = "text" name = 'name' >
password:< input type = "password" name = 'password' >
< input type = "submit" name = 'submit' >
</ form >
< table border = '1px' >
< thead >
< tr >
< td >user</ td >
< td >password</ td >
</ tr >
</ thead >
< tbody >
{%for name in names%}
{%if name[0] and name[1]%}
< tr >
< td >{{name[0]}}</ td >
< td >{{name[1]}}</ td >
</ tr >
{%endif%}
{%endfor%}
</ tbody >
</ table >
|
本文转自 shouhou2581314 51CTO博客,原文链接:http://blog.51cto.com/thedream/1836931,如需转载请自行联系原作者