Steno 就是 yobot。今天我把 Feedjack 掛進 Steno,讓 Steno 可以從 Feedjack 的資料庫裡挖 feed 出來,貼到 IRC 頻道上去。
然後,用 twisted.internet.task.LoopingCall 來定時檢查 Feedjack 資料庫。完成收工,謝謝觀賞。
最近各位有空的話,請儘量狂操 yobot。丟 URL 給她抓、和她聊天,或是拼命寫 Blog 給她 report to channel (Planet@Python.tw 諸君,加油!)
Steno 就是 yobot。今天我把 Feedjack 掛進 Steno,讓 Steno 可以從 Feedjack 的資料庫裡挖 feed 出來,貼到 IRC 頻道上去。
然後,用 twisted.internet.task.LoopingCall 來定時檢查 Feedjack 資料庫。完成收工,謝謝觀賞。
最近各位有空的話,請儘量狂操 yobot。丟 URL 給她抓、和她聊天,或是拼命寫 Blog 給她 report to channel (Planet@Python.tw 諸君,加油!)
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.
www/abc.rpy... that's it. We're done.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()
當然,在利用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 twistdwww-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
加入 /etc/init.d :#!/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
sudo cp twistd /etc/init.d/測一下:
sudo /etc/init.d/twistd沒問題的話,接著加入 rcX.d 吧;有問題的話?google吧。c
* 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.
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!