web

Falcon: Django-like urlreverse router

The default Falcon router don't support urlreverse, but fortunately it allows us to use a costomized router.

Override the add_route, adding a aditional params name to specified url template name.

Note: Falcon version should >=0.4 to support this feature.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from falcon.routing import DefaultRouter


class ReverseRouter(DefaultRouter):
    url_map = {}

    # override add_route to add our map
    def add_route(self, uri_template, method_map, resource, name=None):
        if name:
            self.url_map[name] = uri_template
        DefaultRouter.add_route(self, uri_template, method_map, resource)

    def reverse(self, _name, **kwargs):
        '''
        reverse url
        '''
        assert _name in self.url_map, "url name: %s not in url map" % _name
        url_tmpl = self.url_map.get(_name)
        return url_tmpl.format(**kwargs)

利用dropbox自动备份网站数据

我等小blog网站自然是用不上高大上的备份方案, 但是不备份后果很严重, 万一那天VPS公司跑路了, 或者机器挂了, 东西就全没鸟.

穷人只用的起免费的Dropbox =.=

安装

根据自己安装版本选择32或者64位的, 解压后会有一个.dropbox-dist的隐藏目录.

1
2
3
4
5
6
7
# Stable 32-bit:
wget -O dropbox.tar.gz "http://www.dropbox.com/download/?plat=lnx.x86"
 
# Or stable 64-bit:
wget -O dropbox.tar.gz "http://www.dropbox.com/download/?plat=lnx.x86_64"

tar -xvf dropbox.tar.gz

第一次在cmdline运行的时候, 会提示通过一个链接绑定帐号, 照着做就行了.

1
~/.dropbox-dist/dropbox

然后就会在home目录下建立一个Dropbox的目录.

Setup a mail server to send email only

SMTP server

Here I just choose postfix. Someone on the Internet said the configuration was very complicated, but after I install postfix, I found the default config file is OK if you just want to send email from you locathost.

1
sudo apt-get install postfix

At first time when you test sending email, you might encount with such error:

1
2
postdrop: warning: unable to look up public/pickup: 
No such file or directory

That's because old sendmail daemon still running, just stop it and restart postfix can fix:

1
2
3
$ sudo /etc/init.d/sendmail stop 
$ sudo  mkfifo /var/spool/postfix/public/pickup
$ sudo /etc/init.d/postfix restart

Beautiful soup

对象类型

标签 tag

tag这个概念应该对应于XML的元素(Element), 包括起始与结束标签.

1
2
3
4
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b
type(tag)
# <class 'bs4.element.Tag'>

结构如下:

  • tag
    • name
    • attrs
    • string

部署Django到Lighttpd

Django & FastCGI

软件包依赖

在Django上使用FastCGI需要flup包:

1
pip install flup

运行Django server

Django 内置有runfcgi命令, 建立一个脚本, 运行Django:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
app_path='/path/to/django-app/'
p='/var/run/lighttpd/django-fastcgi.pid'
cd "$app_path"
if [ -f $p ]; then
    kill $(cat -- $p)
    rm -f -- $p
fi

exec /usr/bin/env \
    PYTHONPATH="$app_path/.." python \
    manage.py runfcgi \
    method=threaded \
    host=127.0.0.1 \
    port=3033 \
    pidfile="$p"

这里使用的TCP方式, 使用127.0.0.1:3033 地址.