二月 17, 2007
» Web Programming in the twistd

After "Play twisted as http server on Ubuntu" long long ago, it might need a little more about "how to program in the Twisted web server" before I swimming to play TurboGears.


Alright, let's just start a simple program of random choice.
www/abc.rpy
from twisted.web import resource
from random import choice

choiced = choice('ABC')
content = "this is content of " + choiced

page = '''
<html><head><title>Dynamic Page of ABC</title></head>
<body>
<p>''' + content + '''</p>
</body>
</html>
'''

class Resource(resource.Resource):
def render(self, request):
return page
resource = Resource()

... that's it. We're done.

See also:

八月 25, 2006
» Play twisted as http server on Ubuntu

當然,在利用aptitude裝好twisted之後,才開始動作;我想目前在debian上的作法大概也差不多。

mktap --uid www-data --gid www-data web --path /var/www --port 80
系統測試:
sudo twistd -f web.tap -l /var/log/twistd.log --pidfile /var/run/twistd.pid
ps aux | grep twistd
應會出現類似這樣的東西:
www-data 10675 0.0 3.0 10772 6896 ? S 11:45 0:00 /usr/bin/python2.4 /usr/bin/twistd -f web.tap -l /var/log/twistd.log --pidfile /var/run/twistd.pid
不過最準的測試還是直接用瀏覽器測試:(沒有curl?那就用aptitude裝一下吧。curl -I 好用的。)
curl localhost
應該會得到/var/www/index.html這個網頁的HTML碼。
一切順利的話,就準備來讓它預設啟動吧。
記得先把剛剛啟動的server process幹掉:
sudo killall twistd
確認一下:
ps aux | grep twistd
應該會看到剛剛那個 www-data 10675 0.0 3.0 10772 6896 ? S 11:45 0:00 /usr/bin/python2.4 /usr/bin/twistd ... 的東西才是。 那麼就先把設定檔放到 /etc/ :
sudo cp web.tap /etc/
接下來寫一個script: twistd
內容大概是這樣:
#!/bin/sh
# /etc/init.d/twistd
#

set -e

DAEMON=/usr/bin/twistd
APPFILE=/etc/web.tap
LOGFILE=/var/log/twistd.log
PIDFILE=/var/run/twistd.pid
NAME=twistd

test -x $DAEMON || exit 0
. /lib/lsb/init-functions

#!/bin/sh

case "$1" in
start)
log_begin_msg "Starting HTTP server: $NAME"
start-stop-daemon --start --quiet --oknodo --exec $DAEMON -- -f $APPFILE -l $LOGFILE --pidfile $PIDFILE && log_end_msg 0 || log_end_msg 1
;;
stop)
log_begin_msg "Stopping HTTP server: $NAME"
start-stop-daemon --stop --quiet --pidfile $PIDFILE
# Wait a little and remove stale PID file
sleep 1
if [ -f $PIDFILE ] && ! ps h `cat $PIDFILE` > /dev/null
then
rm -f $PIDFILE
fi
log_end_msg 0
;;
restart)
$0 stop
$0 start
;;
*)
log_success_msg "Usage: /etc/init.d/$NAME {start|stop|restart}"
exit 1
;;
esac

exit 0
加入 /etc/init.d :
sudo cp twistd /etc/init.d/
測一下:
sudo /etc/init.d/twistd
* Usage: /etc/init.d/twistd {start|stop|restart}
sudo /etc/init.d/twistd start
* Starting HTTP server: twistd
...done.
sudo /etc/init.d/twistd restart
* Stopping HTTP server: twistd
...done.
* Starting HTTP server: twistd
...done.
sudo /etc/init.d/twistd stop
* Stopping HTTP server: twistd
...done.
沒問題的話,接著加入 rcX.d 吧;有問題的話?google吧。c
sudo update-rc.d twistd defaults
Adding system startup for /etc/init.d/twistd ...
/etc/rc0.d/K20twistd -> ../init.d/twistd
/etc/rc1.d/K20twistd -> ../init.d/twistd
/etc/rc6.d/K20twistd -> ../init.d/twistd
/etc/rc2.d/S20twistd -> ../init.d/twistd
/etc/rc3.d/S20twistd -> ../init.d/twistd
/etc/rc4.d/S20twistd -> ../init.d/twistd
/etc/rc5.d/S20twistd -> ../init.d/twistd

那麼,下次開機應該會自動啟動了。當然,不必等下次開機,現在就啟動它吧。
sudo /etc/init.d/twistd start
curl -I localhost
HTTP/1.1 200 OK
Date: Sat, 26 Aug 2006 05:40:22 GMT
Last-modified: Fri, 25 Aug 2006 16:51:05 GMT
Content-length: 1296
Content-type: text/html
Server: TwistedWeb/2.2.0
* Congratulation!

biggo.com.tw

A Django site.