1-WEB開發基礎
1-HTTP方法
GET和POST方法
HTTP請求:(GET POST PUT DELETE HEAD OPTIONS)
2-HTTP狀態碼
- 404 Not Found
- 400 Bad Request
- 500 Internal Server Error
- 200 OK
3-HTML頁面生成
import tornado.ioloop
import tornado.web
from tornado.options import define,options #兩個方法
import tornado.options
TEMPLATE = "<html><head></head><body><h1>{}</h1></body></html>"
define('port',default='8000',type=int,help="listening port")
def make_app():
handlers=[
(r'/index',IndexHandler),
(r'/pic',PictureHandler),
(r'/new/pic',PictureHandler),
]
#print(debug) #默認是False
settings ={
'debug':True,
'static_path':'static' ,#相對路徑 相對於執行腳本所在的項目開始
#'static_url_prefix':'/files/' #靜態文件的網址前綴,默認爲"/static/"
}
app = tornado.web.Application(handlers=handlers,**settings)
return app
if __name__ == '__main__':
options.parse_command_line() #處理命令行額外參數 方便從命令行提供的參數
app = make_app()
app.listen(options.port)
print("starting tornado on port {}".format(options.port))
tornado.ioloop.IOLoop.current().start()
結果:
4-客戶端:請求----服務端 響應
2-基本頁面的實現
1-Application對象
-
路由表URL映射
-
關鍵字參數settings
def make_app():
handlers=[
(r'/index',IndexHandler),
(r'/pic',PictureHandler),
(r'/new/pic',PictureHandler),
]
#print(debug) #默認是False
settings ={
'debug':True,
'static_path':'static' ,#相對路徑 相對於執行腳本所在的項目開始
#'static_url_prefix':'/files/' #靜態文件的網址前綴,默認爲"/static/"
}
app = tornado.web.Application(handlers=handlers,**settings)
return app
2-RequestHandler子類
- 主入口點:處理HTTP方法
- 產生響應:1-render或者write 2-錯誤處理或者重定向
- 可供複寫的方法
1-每個請求的調用序列
2-常用的複寫方法
3-常用的複寫方法
RequestHandler.initialize() 子類初始化
RequestHandler.prepare() 在每個請求的最開始被調用, 在 get/post/等方法之前.
通過複寫這個方法, 可以執行共同的初始化, 而不用考慮每個請求方法.
RequestHandler.on_finish()
在一個請求結束後被調用.
RequestHandler.get(*args, **kwargs)
RequestHandler.head(*args, **kwargs)
RequestHandler.post(*args, **kwargs)
RequestHandler.delete(*args, **kwargs)
RequestHandler.put(*args, **kwargs)
4-實現首頁以及圖片
首先在項目根目錄下創建static目錄 在static目錄下創建images目錄 images目錄存放本地圖片
則需要在settings中配置好static_path
import tornado.ioloop
import tornado.web
from tornado.options import define,options #兩個方法
import tornado.options
#提供靜態文件訪問
TEMPLATE = "<html><head></head><body><h1>{}</h1></body></html>"
define('port',default='8000',type=int,help="listening port")
class IndexHandler(tornado.web.RequestHandler):
def get(self):
print('get=========')
self.write('<html><head></head><body><h1>Index Page</h1></body></html>')
def initialize(self): #最先執行 不管函數順序
print(self)
class PictureHandler(tornado.web.RequestHandler):
def get(self):
self.write(TEMPLATE.format(
#"<img src='https://www.tornadoweb.org/en/stable/_images/tornado.png' />"
'<img src="/static/images/cat1.jpg" />' #相對路徑圖片加載
))
def make_app():
handlers=[
(r'/index',IndexHandler),
(r'/pic',PictureHandler),
(r'/new/pic',PictureHandler),
]
#print(debug) #默認是False
settings ={
'debug':True,
'static_path':'static' ,#相對路徑 相對於執行腳本所在的項目開始
#'static_url_prefix':'/files/' #靜態文件的網址前綴,默認爲"/static/"
}
app = tornado.web.Application(handlers=handlers,**settings)
return app
if __name__ == '__main__':
options.parse_command_line() #處理命令行額外參數 方便從命令行提供的參數
app = make_app()
app.listen(options.port)
print("starting tornado on port {}".format(options.port))
tornado.ioloop.IOLoop.current().start()
請求後 給出的響應是
當然 可以直接訪問
在settings中配置的好處是:相對路徑 相對於執行腳本所在的項目開始
若需要改變默認的靜態文件存放目錄 則需要在settings中寫入參數
settings ={
'debug':True,
'static_path':'static' ,#相對路徑 相對於執行腳本所在的項目開始
'static_url_prefix':'/files/' #靜態文件的網址前綴,默認爲"/static/"
}
執行效果
完整代碼如下:
import tornado.ioloop
import tornado.web
from tornado.options import define,options #兩個方法
import tornado.options
TEMPLATE = "<html><head></head><body><h1>{}</h1></body></html>"
define('port',default='8000',type=int,help="listening port")
class IndexHandler(tornado.web.RequestHandler):
def get(self):
print('get=========')
self.write('<html><head></head><body><h1>Index Page</h1></body></html>')
def initialize(self): #最先執行 不管函數順序
print(self)
class PictureHandler(tornado.web.RequestHandler):
def get(self):
self.write(TEMPLATE.format(
#"<img src='https://www.tornadoweb.org/en/stable/_images/tornado.png' />"
'<img src="/static/images/cat1.jpg" />' #相對路徑圖片加載
))
def make_app():
handlers=[
(r'/index',IndexHandler),
(r'/pic',PictureHandler),
]
#print(debug) #默認是False
settings ={
'debug':True,
'static_path':'static' ,#相對路徑 相對於執行腳本所在的項目開始
'static_url_prefix':'/files/' #靜態文件的網址前綴,默認爲"/static/"
}
app = tornado.web.Application(handlers=handlers,**settings)
return app
if __name__ == '__main__':
options.parse_command_line() #處理命令行額外參數 方便從命令行提供的參數
app = make_app()
app.listen(options.port)
print("starting tornado on port {}".format(options.port))
tornado.ioloop.IOLoop.current().start()