Dust8 的博客

读书百遍其义自见

0%

把python换成3了,网上的bencode基本不能用了。

因为 types 里已经没了那些类型, 如 IntType等等。

而且python3里只有一种整数类型,不分 intlong了。

所以只好自己改造下。 Bencode编解码的python代码这里有详细的讲解。

大体差不多,一些错误判断就没写了。

下面是我改的, github里是 b.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/env python3


def bdecode(x):
try:
r, l = decode_func[x[0]](x, 0)
return r
except (IndexError, KeyError, ValueError):
pass


def decode_int(x, f):
f += 1
newf = x.index("e", f)
n = int(x[f:newf])
return (n, newf+1)


def decode_string(x, f):
coln = x.index(":", f)
n = int(x[f:coln])
coln += 1
return (x[coln:coln+n], coln+n)


def decode_list(x, f):
r,f = [], f+1
while x[f] != "e":
v, f = decode_func[x[f]](x, f)
r.append(v)
return (r, f+1)


def decode_dict(x, f):
r, f = {}, f+1
while x[f] != "e":
k, f = decode_string(x, f)
r[k], f = decode_func[x[f]](x, f)
return (r, f+1)


decode_func = {}
decode_func['l'] = decode_list
decode_func["d"] = decode_dict
decode_func['i'] = decode_int
decode_func['0'] = decode_string
decode_func['1'] = decode_string
decode_func['2'] = decode_string
decode_func['3'] = decode_string
decode_func['4'] = decode_string
decode_func['5'] = decode_string
decode_func['6'] = decode_string
decode_func['7'] = decode_string
decode_func['8'] = decode_string
decode_func['9'] = decode_string


def bencode(x):
r = []
encode_func[type(x)](x, r)
return "".join(r)


def encode_int(x, r):
r.extend(("i", str(x), "e"))


def encode_string(x, r):
r.extend((str(len(x)), ":", x))


def encode_list(x, r):
r.append("l")
for i in x:
print(type(i))
encode_func(type(i))(i, r)
r.append("e")


def encode_dict(x, r):
r.append("d")
ilist = sorted(x.items())
ilist.sort()
for k, v in ilist:
r.extend((str(len(k)), ":", k))
encode_func[type(v)](v, r)
r.append("e")


encode_func = {}
encode_func[type(3)] = encode_int
encode_func[type("")] = encode_string
encode_func[type([])] = encode_list
encode_func[type(())] = encode_list
encode_func[type({})] = encode_dict


if __name__ == "__main__":
e = {"t": "aa", "y": "q", "q": "ping", "a": {"id": "abcdefghij0123456789"}}
d = "d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe"

assert bencode(e) == d
assert bdecode(d) == e

tornado 自定义错误页, 网上有好几种方法, 都不是很好。

我发现还可以这样改, 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import tornado.ioloop
import tornado.web

class BaseHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
self.finish("this is my 404!")

class MainHandler(BaseHandler):
def get(self):
self.write("Hello, world")

application = tornado.web.Application([
(r"/", MainHandler),
])

if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

就是重载 tornado.web.RequestHandler, 重写 write_error 方法。

然后根据 status_code 的状态码给出不同的响应。

不过 finish 有效一点, 那些 renderredirect 都有些问题。

中文文件名乱码产生的原因有二:

一是挂载NTFS或FAT文件系统时,编码指定不正确导致乱码(或问号); 二是在文件系统中文件名存储的编码不正确,导致乱码。

详细介绍可以看 中文文件名乱码问题。下面说说第二种情况。

文件是在WIndows 下创建的,Windows 的文件名中文编码默认为GBK,而Linux中默认文件名编码为UTF8,由于编码不一致所以导致了文件名乱码的问题,解决这个问题需要对文件名进行转码。

1.安装 convmv

1
sudo apt-get install convmv

2.convmv简介

convmv存在于常见操作系统的软件仓库中。如果当前没有工具,可以直接安装。

1
convmv -f 源编码 -t 新编码 [选项] 文件名

常见有用的选项有:

1
2
3
-r 递归处理子文件夹
--notest 真正进行操作,请注意在默认情况下是不对文件进行真实操作的,而只是试验。
--unescape 可以做一下转义,比如把%20变成空格,如果你不知道这是在做什么,就不要尝试了。

3.实例

先试着将gbk文件名改为UTF-8:

1
convmv -f gbk -t utf8 -r MY_DIR

如果确认输出无误:

1
convmv --notest -f gbk -t utf8 -r MY_DIR[MY_DIR是目录]

其他

1
convmv --notest -f gbk -t utf8 -r *[把当前文件夹下所有乱码文件名改过来]

1.安装Nginx

1
apt-get install nginx

所有的配置文件都在 /etc/nginx 下,

每个虚拟主机配置文件在 /etc/nginx/sites-available 下,

默认的虚拟主机的目录设置在了 /usr/share/nginx/www,

启动程序文件是/usr/sbin/nginx,

启动脚本 nginx 在/etc/init.d/ 下, 日志在 /var/log/nginx 中,分别是 access.log 和 error.log。

2.安装pip

1
apt-get install python-pip

3.安装supervisor

文档可以看 http://supervisord.org/installing.html#installing-via-pip

1
2
pip install --upgrade supervisor  
echo_supervisord_conf > /etc/supervisord.conf

4.安装virtualenv

1
pip install --upgrade virtualenv

5.安装virtualenv虚拟环境

1
2
3
virtualenv tblogenv  
source tblogenv/bin/activate
pip install tornado

6.配置nginx

这是 /etc/nginx/sites-enabled/tblog 里的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server{
listen 80;
server_name dust8.com www.dust8.com blog.dust8.com;

location ^~ /static/{
root /usr/www/tblog;
if ($query_string) {
expires max;
}
}

location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://127.0.0.1:8888;
}
}

7.配置supervisor

在 /etc/supervisord.conf 后面加入

1
2
3
4
5
6
[program:tblog-8888]
command=/usr/www/tblogenv/bin/python /usr/www/tblog/app.py
numproce=2
autostart=true
autorestart=true
stdout_logfile=/usr/www/tblog/python_log

8.重启nginx和supervisor

1
2
/etc/init.d/nginx restart
supervisorctl restart all

最近又把笔记本搞成ubuntu13.04了,除了安装amd官网的显卡驱动解决发热问题, 还把亮度不能保存问题给解决了。

1.按ctrl+alt+t可以唤出终端,在终端里输入

1
sudo gedit /etc/rc.local

2.在 exit 0 前面输入下面的东东:

1
2
chmod a+w /sys/class/backlight/acpi_video0/brightness    
echo 80 > /sys/class/backlight/acpi_video0/brightness

备注:

1
echo 80

这个 80 是根据

1
/sys/class/backlight/acpi_video0/max_brightness

这个最大亮度决定的,因为我的是100.