灵感来源:https://code.google.com/p/mentohust/issues/detail?id=207
终于又周五了,上课上的蛋疼,下午上完课不想看扯淡的全英论文,就又想起来上面那个地址里面提到的问题了
刚开始尝试直接在路由上安装个nginx,然后
ln -s /jffs/tmp/mentohust.log /jffs/www/index.log
浏览器打开一看,我去,不光乱码,换行都没
又尝试在log文件开头添加一句
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
擦,大部分不乱码了,但是最关键的系统通提示乱了。。。。。。。蛋疼,并且还没换行
(系统通告是GBK编码,其他部分是UTF-8 )
然后我想去改下mentohust代码,在输出到文件的时候自行转码,用libiconv应该就行,然后发现输出到文件是用重定位stdout流实现的,,,,
艹艹艹草,这你嘛的让我怎么改啊,让我重写printf啊!!!!
算了,放弃,,,,
然后 下面的代码就出来了。。。
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from cStringIO import StringIO
def getbuf():
buf = ''
def c(line):
tmp = line.decode("utf-8")
tmp = tmp.encode("gb2312")
return tmp
def rl(line):
try:
return c(line)
except:
p = line.strip().split()
try:
p[1] = c(p[1])
except:
pass
return ' '.join(p)
with open("/jffs/tmp/mentohust.log") as f:
for line in f:
tmp = rl(line)
if tmp[0] != tmp[1]:
buf += '$$ \xcf\xb5\xcd\xb3\xcc\xe1\xca\xbe: '
buf += tmp + '<br>'
return buf
class TestHTTPHandle(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/sss":
self.protocal_version = "HTTP/1.1"
self.send_response(200)
self.send_header("Content-type", "text/html; charset=gb2312")
self.end_headers()
self.wfile.write(getbuf())
else:
self.send_error(404)
http_server = HTTPServer(('192.168.1.1', int(80)), TestHTTPHandle)
http_server.serve_forever()
最蛋疼的一点就是 系统提示那一行是UTF-8和GBK都有,蛋疼的要死,只能except: 出来单独处理了。。。
这个脚本的性能,还行,在500主频的5358上刷新一次网页只占不到2%的CPU,内存几乎没占
我在实验室打开宿舍的路由,效果图:
PS:我路由器现在的启动脚本,各种小玩意,DDWRT真耐玩。。
PPS:如果遇到路由器空间过小无法安装完整的python,参见下面链接解决,最好的还是挂个U盘,就像上图所示,我在/JFFS挂了个8G U盘,随便玩了。
当然,想顺畅的跑起来64MB的内存是必须的,如果是32MB的路由,别折腾了,卡死
http://www.yonsm.net/post/645
https://code.google.com/p/wallproxy-plus/issues/detail?id=145&can=1&q=%E7%B2%BE%E7%AE%80