python的SimpleHTTPServer

想快速的共享些文件给其他人? 用samba,或者简单的起个web server即可。 serv(){ ip=`ifconfig eth0 | sed -n 's/^\s*inet [^:]*://p' | awk '{print $1}'`; echo http://$ip:8000; python -m SimpleHTTPServer; } 加到~/.bashrc中,以后在需要的目录中运行serv 550 /tmp# serv http://10.16.22.1:8000 Serving HTTP on 0.0.0.0 port 8000 ... wang-desktop.local - - [04/Apr/2010 16:17:22] "GET / HTTP/1.1" 200 - wang-desktop.local - - [04/Apr/2010 16:17:22] "GET / HTTP/1.1" 200 - 然后访问把地址, 如“http://10.16.22.1:8000”发给其他人即可。 ...

April 4, 2010 · notsobad

python改变工作目录

python脚本执行时,有一个当前工作目录,用不同方式启动,是不一样的 如在存在一个 /tmp/x.py cd /tmp; python x.py cd $HOME; python /tmp/x.py 两种执行方式,当前工作目录是不一样的,内部代码涉及到路径问题的话,需要自己处理 import os,sys print os.getcwd() os.chdir(os.path.dirname(sys.argv[0])) # 切换到脚本所在的目录 print os.getcwd()

March 29, 2010 · notsobad

python捕获stdout输出

python中捕获到stdout的输出,实际上就是把sys.stdout只想到一个打开的文件即可 import StringIO import string, sys stdout = sys.stdout sys.stdout = file = StringIO.StringIO() print """ According to Gbaya folktales, trickery and guile are the best ways to defeat the python, king of snakes, which was hatched from a dragon at the world's start. -- National Geographic, May 1997 """ sys.stdout = stdout print string.upper(file.getvalue()) 类似与php中的ob_*系列函数。 It's like comparing apples to oranges.

March 13, 2010 · notsobad

Beautifulsoup的unicode问题

python的unicode问题实在是让人痛苦,本身要写一段小程序,时间都被浪费在处理unicode上面了。 我的python版本 python -c 'import sys;print sys.version' 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] import os,sys from BeautifulSoup import BeautifulSoup, SoupStrainer def get_info(cont): print type(cont) soup = BeautifulSoup(cont) a = soup.findAll('a') print type(a) print(a) if __name__ == "__main__": s = sys.stdin.read() s = unicode(s, 'utf-8') get_info(s) 出错信息: 505 ~/script/notsobad/python/tool>cat /tmp/book_2742/index.html | ./book_res.py Traceback (most recent call last): File "./book_res.py", line 28, in get_info(s) File "./book_res.py", line 20, in get_info print(a) UnicodeEncodeError: 'ascii' codec can't encode characters in position 79-82: ordinal not in range(128) type(a) 是unicode,但是print a却报错。 最后在python的mail list里找到了篇 http://mail.python.org/pipermail/tutor/2005-August/040991.html http://mail.python.org/pipermail/tutor/2005-August/040993.html ...

January 9, 2010 · notsobad

webfaction中django问题

webfaction中django配置,通用的没什么可讲的,只是有几点需要注意的. webfaction的web目录不能是一个链接,必须是一个实际的目录. 修改了除模板,media以外的任意文件,都要重启apache。 为了方便,把几个bin目录加到PATH里 把这一行加入~/.bashrc ...

November 26, 2009 · notsobad

webfaction中使用crontab与安装python包

继续感冒中…… 实在睡不着了,把我的这个脚本移到webfaction,放到crontab里了。 设置PYTHONPATH和PATH环境变量 [notsobad@web108 ~]$ cat .bashrc # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # User specific aliases and functions export PYTHONPATH=$PYTHONPATH:$HOME/script/python-lib export PATH=$PATH:$HOME/script/bin 然后就可以使用easy_install了,我要安装feedparser: easy_install --install-dir=$HOME/script/python-lib --script-dir=$HOME/script/bin feedparser crontab的使用: crontab -e 直接编辑即可,注意crontab使用的环境变量和登录用户使用的是不一样的,要使用自定义的pythonpath的话,需要自己指定 ...

November 8, 2009 · notsobad

什么是好的命令行程序

最近处理bug,总结一下什么是好的命令行程序: 无参数调用时,显示帮助 记录日志到文件 关键步骤要打印log到标准输出 出错提示信息要写到标准错误里去 要有退出值 python程序不要屏蔽异常,要打印到stderr里去 脚本初始时要明确的检验执行权限、目录是否满足需求 python程序最好不要用PYTHONPATH来设置包含路径,移植和多人合作问题太多,应该直接链接到site-packages下去 ...

August 14, 2009 · notsobad

用gnome-web-photo进行网页截屏

参考下这里: http://ubuntuforums.org/showthread.php?t=1085872 还有这里: [http://groups.google.com/group/python- cn/browse_thread/thread/f25350d5e4d415af/08140024364b819d?lnk=gst&q;=html#](http://groups.google.com/group/python- cn/browse_thread/thread/f25350d5e4d415af/08140024364b819d?lnk=gst&q=html#) 使用gnome-web-photo进行网页截屏,效果相当好,不过保存为jpg格式失败了 514 ~/script/sh>gnome-web-photo --format "png" http://blog.notsobad.cn blog.png Registering '@mozilla.org/module-loader/python;1' (libpyloader.so) Registering '@mozilla.org/network/protocol/about;1?what=python' (pyabout.py) 515 ~/script/sh>animate blog.png 516 ~/script/sh>gnome-web-photo --mode=photo --format "jpeg" http://blog.notsobad.cn blog.jpg Registering '@mozilla.org/module-loader/python;1' (libpyloader.so) Registering '@mozilla.org/network/protocol/about;1?what=python' (pyabout.py) JPEG parameter struct mismatch: library thinks size is 372, caller expects 356

August 3, 2009 · notsobad

python实现一个strip_tags和unicode笔记

python实现一个strip_tags, 去处html标记 from HTMLParser import HTMLParser def strip_tags(html): result = [] parser = HTMLParser() parser.handle_data = result.append parser.feed(html) parser.close() return ''.join(result) 关于unicode: 参考这里 http://evanjones.ca/python-utf8.html 总结:出现编码错误时,用type检查变量的类型,容易找出问题,print是不行的 unicote <--> str, 互相转换 In [28]: a=‘a’ In [29]: type a -——> type(a) Out[29]: In [30]: b=unicode(a, ‘utf-8’) In [31]: type b -——> type(b) Out[31]: In [32]: b.decode(‘utf-8’) Out[32]: u’a’ In [33]: b.encode(‘utf-8’) Out[33]: ‘a’ ...

August 3, 2009 · notsobad

php模块的调试

python里面有这样的用法 #a.py if __name__ == "__main__": print "test" //只有直接python a.py 才会打印test 意思就是当前脚本独立运行时,才会运行print “test”,这在编写模块,并被其他程序调用时,可以用来调试。 php中没有对应的,今天想起来可以这样做: ...

June 18, 2009 · notsobad