1. 程式人生 > >python tornado 搭建webServer

python tornado 搭建webServer

1.安裝tornado,我用的windows環境
在tornado網站上下載tornado:
https://pypi.python.org/packages/source/t/tornado/tornado-4.2.tar.gz
解壓tornado-4.2.tar.gz
在解壓後的tornado-4.2資料夾中執行命令:
python setup.py install
完成安裝。

3.學習筆記
最基礎的POST和GET的方法實現

# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web

html = '''
<form method="post" name="frm1" action="/login">
    <label for="txt">使用者名稱</label>
    <input type="text" id="txtname" name="myname">
<br/>
<br/>
    <label for="txt">密碼  </label>
    <input type="text" id="txtpwd" name="mypwd">
<br/>
<br/>
    <input type="submit">
</form>
'''
class BaseHandler(tornado.web.RequestHandler): def get_current_user(self): return self.get_secure_cookie("user") class MainHandler(BaseHandler): def get(self): if not self.current_user: self.redirect("/login") return name = tornado.escape.xhtml_escape(self.current_user) self.write("Hello, "
+ name) class LoginHandler(BaseHandler): def get(self): self.write(html) def post(self): self.set_secure_cookie("user", self.get_argument("myname")) # self.write("POST LOGIN") self.redirect("/") settings = dict( # template_path=TEMPLATE_PATH, # static_path=STATIC_PATH,
# cookie_secret=str(uuid.uuid1()), cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", login_url="/login", # gzip=True, # xheaders=True, debug=True ) application = tornado.web.Application([ (r"/", MainHandler), (r"/login", LoginHandler) ], **settings) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.current().start()