顯示具有 web framework 標籤的文章。 顯示所有文章
顯示具有 web framework 標籤的文章。 顯示所有文章

2012/02/29

Tornado web framework + HTTPS

從官網得知 Tornado web framework 原本就有支援SSL協定
HTTPServer can serve SSL traffic with Python 2.6+ and OpenSSL. To make this server serve SSL traffic, send the ssl_options dictionary argument with the arguments required for the ssl.wrap_socket method, including “certfile” and “keyfile”:
    HTTPServer(applicaton, ssl_options={
        "certfile": os.path.join(data_dir, "mydomain.crt"),
        "keyfile": os.path.join(data_dir, "mydomain.key"),
    })

SSL Sample
import os
from tornado.httpserver import HTTPServer
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop

class MainHandler(RequestHandler):
    def get(self):
        self.write("Hello world!")

def main():
    application = Application([
        (r"/", MainHandler), 
    ])  

    http_server = HTTPServer(application,  
        ssl_options={
            "certfile": os.path.join(os.path.abspath("."), "test.crt"), 
            "keyfile": os.path.join(os.path.abspath("."), "test.key"), 
        }   
    )

    http_server.listen(8080)
    IOLoop.instance().start()

if __name__ == "__main__":
    main()

過程中需要ssl certificate 與 private key,可以用openssl產生,不過過程很繁雜,建議可以利用online tool:Create self-signed SSL certificates online

測試
開啓 http://localhost:8080 遇到錯誤:
ERROR:root:Exception in I/O handler for fd 3 Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/tornado-2.0-py2.6.egg/tornado/ioloop.py", line 287, in start self._handlers[fd](fd, events) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/tornado-2.0-py2.6.egg/tornado/stack_context.py", line 159, in wrapped callback(*args, **kwargs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/tornado-2.0-py2.6.egg/tornado/httpserver.py", line 294, in _handle_events except ssl.SSLError, err: AttributeError: 'module' object has no attribute 'SSLError' 

少了甚麼module嗎?
重裝openssl 0.9.8t -> 不行


安裝ssl package
# pip install ssl
ValueError: This extension should not be used with Python 2.6 or later (already built in), and has not been tested with Python 2.3.4 or earlier.
python 2.6以後的版本已經內建了


安裝pyopenssl package
# pip install pyopenssl
安裝成功,但結果還是一樣。看起來該有的都有了阿!


最後把deploy系統從mac os換到centos 5,一模一樣的步驟就...成功了!上面的問題至今未解...


如果看到這個warning,表示client的request是用http不是https,並不是server的問題唷!
WARNING:root:SSL Error on 7: [Errno 1] _ssl.c:490: error:1407609C:SSL routines:SSL23_GET_CLIENT_HELLO:http request

2011/09/22

Implement tornado session in memcached

示範將tornado的session實作在memcached裡面,我是參考這個blog:
玩蛇记-给tornado加上session支持 - 亚历山大同志 - 博客园

Requirement
  • Tornado 2.0
  • python-libmemcached 0.4
    • Pyrex 0.9.4+
    • libmemcached 0.52

邏輯


流程

2011/09/19

初學 Tornado web framework

最近需要架設一個線上平台,對於架設平台我實在沒什麼經驗,從前有開發過網站,會用asp.net、php自己刻東西,但時代不一樣了,為了講求快速,依附在framework上是有必要的。Tornado — 對我來說是個全新的開始,我不會python,對物件導向的程式語言也不熟,平常都在寫C語言居多,更別提用過web framework了...但我堅信我的底子跟熱情可以支撐我去學習這一切!

話說剛開始學習真的超不順,中文英文的教學都很少,官方的文件又寫的很簡略...但看著看著,突然有一天感覺就來了,就像任督二脈被打通般的,我看得懂了,我會使用了,程式寫這麼久了,我覺得對我幫助最大就是學會這種感覺。如果看懂Overview又學會使用Source code裡面的demo程式,對Tornado的掌握度就很高了!

API
Tornado真是個強大的東西,它陳列的API不多,但每一個都是在建立平台時很常見的需求:
  • OAuth第三方認證 - 使用facebook、google、twitter帳號來登入自己的網站,API都很精簡了
  • Python decorator tornado.web.authenticated - Sorry我不知道怎麼翻譯這個,簡單來說就是user尚未通過認證,存取到任何網頁都會被導回login頁面
  • Non-blocking, asynchronous http request - facebook graph API query、backend server query
  • Template - MVC架構開發,這是每個framewrok都必備的
  • Security cookie - 防止cookie被篡改
  • L10N, CSRF protect

IDE選擇

2011/07/21

Django初體驗

Web開發技術真是越來越強大了,一段時間沒碰就跟不上了,此時我的技術力還停留在ASP.Net 2.0與純PHP的時代... 快速開發與MVC架構似乎是framework的最大訴求,最近碰到要寫網頁,在想要選擇什麼走什麼路好,雖然有些許PHP底子,但最後是擔心PHP framework performance的問題,選擇了Python Django,開啟新的學習旅程~

第一步:Quick Start 照著做一次

第二步:了解MTV概念與專案內的manage.py settings.py urls.py各是做什麼用的

第三步:



Oh no! 遇到一些麻煩...
django-nonrel是用來補足Django NoSQL這一塊,目前支援的NoSQL有 MongoDB, ElasticSearch, Cassandra,但Amazon SimpleDB還沒完成阿!只好被迫放棄. (雖然說有找到 boto - Python interface to Amazon Web Services 提供了SimpleDB的存取方式,但就用不到ORM的特性了)


沒想到初體驗這麼快就結束了...