install web.py 接口框架用到的包html
http://webpy.org/tutorial3.zh-cn 官方網址python
http://webpy.org/tutorial3.zh-cnweb
學學服務端編程的主題編程
pip install web.py服務器
Urls=()路由app
App表示路由在程序應用裏全局生效框架
類裏定義訪問跟程序的對應關係,就是路由post
def Get 表示對GET的請求作處理學習
Action表單元素的value所提交的網頁,用來驗證捕獲get或post的值是否正確,例如帳戶信息ui
# -*- coding: utf-8 -*-
import web
from web import form
import cgi
#動態的往模板(templates是一個靜態網頁)裏填充數據
render = web.template.render('templates/')
路由,訪問跟程序的對應關係
urls = (
#’http://127.0.0.1:8080/’默認沒加任何地址,會調用下邊的index類的GET方法
'/', 'index',
#’http://127.0.0.1:8080/interface’加了/interface,會調用下邊的interface類的GET或post方法
'/interface', 'interface'
)
app = web.application(urls, globals())
class index:
def GET(self):
#return "Hello, world!"
#print "hello world"
#form = myform()
# make sure you create a copy of the form by calling it (line above)
# Otherwise changes will appear globally
#print(form.render())
return render.formtest()#返回模板頁面,沒有任何東西
class interface:
def GET(self): return "hi"
def POST(self):
i = web.input()#是一個字典
print 「i:」,i
return 'form value:',i.title#把字典key名爲title的值給取到
if __name__=="__main__":
web.internalerror = web.debugerror
app.run()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>haha</title>
</head>
<body>
<form method="post" action="interface">
<p><input type="text" name="title" /> <input type="submit" value="submit" /></p>
</form>
</body>
</form>
</html>
D:\test\study>python interface_index.py
http://127.0.0.1:8080/interface
# -*- coding: utf-8 -*-
import web
from web import form
import cgi
render = web.template.render('templates/')
#路由
urls = (
'/', 'index',
'/interface', 'interface'
)
app = web.application(urls, globals())
class index:
def GET(self):
#return "Hello, world!"
#print "hello world"
#form = myform()
# make sure you create a copy of the form by calling it (line above)
# Otherwise changes will appear globally
#print(form.render())
return render.formtest()
class interface:
def GET(self): return "hi"
def POST(self):
i = web.input()
print "i:",i
return 'form value:',i.title,i #form value是字符串
if __name__=="__main__":
web.internalerror = web.debugerror
app.run()
後臺:
i: <Storage {'title': u'helloxia'}>
127.0.0.1:49925 - - [18/Jun/2018 22:36:05] "HTTP/1.1 POST /interface" - 200 OK
這就是介紹了一下服務器端是怎麼工做的