Python应用中的一些小技巧

定位Python程序的性能

1
2
python -m cProfile xxx.py
python -m profile xxx.py

备注:建议使用cProfile就好了,cProfile会更高效,因为它是用C开发的。

使用pdb调试Python程序

1
python -m pdb xxx.py

详细内容参见《Python中DEBUG调试模块PDB使用》

使用Python快速启动HTTP服务器

1
python -m SimpleHTTPServer

使用Python快速搭建FTP服务器

1
2
3
pip install pyftpdlib
python -m pyftpdlib #启动一个可以匿名登录的FTP服务器
python -m pyftpdlib -u wahaha -P test #启动一个需要使用账号wahaha,密码test登录的FTP服务器

使用Python格式化输出json数据

1
2
cat data.json | python -m json.tool
python -m json.tool data.json

查看Python解释器的目录

1
2
whereis python
which python

查看Python的安装目录

1
2
import sys
print sys.prefix

快速启动一个Python进程

1
echo 'while True: pass' | python &