Posts Tagged ‘Pylons’

化整為零的次世代網頁開發標準: WSGI

一月 27th, 2010

今天,我要介紹Python網頁開發的標準: WSGI,我個人在看見這類英文縮寫時,都一定會試著去記住它的全寫,因為縮寫本身一點意義都沒有,難以記憶,WSGI的全寫是』Web Server Gateway Interface『,它的發音有點像是whiskey,光知道這個名字還是很難理解這到底是用來做什麼用的,簡單的來說,它是Python定義網頁程式和伺服器溝通的介面,如果你有寫過CGI (Common Gateway Interface),它的作用基本上就是和CGI類似的功用,定義一個標準的溝通方式,讓你寫的程式可以和伺服器溝通,但是WSGI不是設計用來給任何語言使用的,它是設計給Python用的,而它其實是基於CGI的延伸,在Python的部份進一步做更多的定義,而因為他是基於CGI,所以它也可以和CGI的介面相容,只要透過一個轉接器,就能把WSGI的程式接到CGI,說了這麼多,相信大部份人對於WSGI是什麼還是一頭霧水,會有一堆疑問,為什麼有了CGI還要有WSGI? Middleware又是什麼? 這很正常,我一開始也對WSGI一點概念都沒有,接下來我們就來介紹WSGI的特色。

» Read more: 化整為零的次世代網頁開發標準: WSGI

Autostart script for TurboGears2

九月 20th, 2009

When I complete a TurboGears2 application, I got a problem, how to keep my tg2 application always running on Webfaction? You know, there is scheduled downtime of virtul host. Therefore, if you just run 『paster serve –daemon production.ini』, once the machine down, your application is down, too. So you have to find a way to keep your tg2 application up. I can use crontab to check tg2 application every 5 minutes, but during the just-start-up 5 minutes, your application is not working, so it is not a good idea. I notice Webfaction use a autostart CGI script for TurboGears1 application. So I decide to use that autostart CGI script to run my tg2 application. You might ask, what is autostart and how it works? Autostart script is a simple script to keep your web application up. It is executed when mod_rewrite of apache can’t connect to your server. By using autostart script, the application runs on-demand! If there are no users browse your application, it’s no need to run your application, that saves your memory usage.

However, the autostart Webfaction provided is for tg1, so I modify it for tg2, here is the modified version of autostart:

<![CDATA[
#!/bin/env python2.4
import os
 
# Test if the process is already running
running = False
# read status of tg2 application
lines = os.popen(
    'source /home/victorlin/webapps/tiange/tg2env/bin/activate;'
    'cd /home/victorlin/webapps/tiange/tiange/tiange;'
    'paster serve  status production.ini').readlines()
line = lines[0]
if line.startswith('Server running in PID'):
    running = True
 
print "Content-type: text/html\r\n"
if running:
    print """<head><META HTTP-EQUIV="Refresh" CONTENT="2; URL=."></head><body>
    Site is starting ...<a href="." mce_href=".">click here<a></body>"""
else:
    print """<head><META HTTP-EQUIV="Refresh" CONTENT="2; URL=."></head><body>
    Restarting site ...<a href="." mce_href=".">click here<a></body>"""
    os.system(
        'source /home/victorlin/webapps/tiange/tg2env/bin/activate;'
        'cd /home/victorlin/webapps/tiange/tiange/tiange;'
        'paster serve  daemon production.ini')
]]>


By the way, you can also use this script for Pylons applications. I hope this simple autostart script would be helpful :D