Intelligence without ambition is a bird without wings.

2014-12-05
python添加模块搜索路径

代码

1
2
3
import sys
sys.path.append( <path to dirFoo> )
import Bar

参考

  1. http://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path
阅读此文

2014-12-05
python-字符串和字典转换

字典 => 字符串

1
2
3
dict1 = {'one':1, 'two':2, 'three': {'three.1': 3.1, 'three.2': 3.2 }}
str1 = str(dict1)
print(str1)

字符串 => 字典

1
2
3
4
5
dict1 = {'one':1, 'two':2, 'three': {'three.1': 3.1, 'three.2': 3.2 }}
str1 = str(dict1)

dict2 = eval(str1)
print(dict2)

参考

  1. http://stackoverflow.com/questions/4547274/convert-a-python-dict-to-a-string-and-back
阅读此文

2014-12-05
python获取IP对应的MAC

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def get_mac_from_ip(ip):
"""
Return the mac from ip in windows.
"""

from subprocess import Popen, PIPE
import re
Popen(["ping", "-c 1", ip], stdout=PIPE, stderr=PIPE)
pid = Popen(["arp", "-a", ip], stdout=PIPE, stderr=PIPE)
s = pid.communicate()[0]
if s is None:
raise IOError("can't get mac from ip(%s)!" % ip)
try:
mac = re.search(r"(([a-f\d]{1,2}\-){5}[a-f\d]{1,2})", s).groups()[0]
except AttributeError:
raise IOError("can't find mac from ip(%s)!" % ip)
return mac

print(get_mac_from_ip('100.100.102.300'))

参考

  1. http://snipplr.com/view/70832/
阅读此文

2014-12-04
优秀软件列表

文件工具

桌面工具

  • f.lux

    随时间调节显示器色温

  • keytweak

    快捷键修改工具

  • rainmeter

    桌面自定义工具

  • VirtualBox

    开源免费虚拟机

  • PotPlayer

    PotPlayer是 KMPlayer作者新作品,全格式、硬件加速,口碑俱佳

系统工具

nirsoft

网络类

  • sokit

    网络测试工具,可以用来接收,发送或者转发TCP/UDP数据包,使用C++语言,QT基础库开发(QT SDK v4.7)。在32位windows及linux系统下编译通过。

  • wireshark

    抓包工具

  • tftpd32

    tftp客户端,服务器, DHCP服务器

  • scp[windows]

    scp, sftp, ftp客户端

  • proxifier

    windows全局代理工具

  • webzip

    整站下载工具

远程控制

  • teamviewer

    穿透内网和防火墙的远程控制!

markdown

画图

开发类

阅读此文

2014-12-04
python-json操作

解码(decoding):

把Json格式字符串解码转换成Python对象

  • 从字符串中
1
2
data = '{"id": 1, "name": "Tom"}'
json_data = json.loads(data)
  • 从文件流
1
2
data = '{"id": 1, "name": "Tom"}'
json_data = json.load(data)

编码(encoding):

把一个Python对象编码转换成Json字符串

  • 转为python字符串
1
json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])

参考

  1. https://docs.python.org/2/library/json.html
阅读此文

2014-12-04
python调试手段

发生异常时,自动打开IPython

  • crash_on_ipy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import sys


class ExceptionHook:
instance = None

def __call__(self, *args, **kwargs):
if self.instance is None:
from IPython.core import ultratb
self.instance = ultratb.FormattedTB(mode='Plain',
color_scheme='Linux', call_pdb=1)
return self.instance(*args, **kwargs)

sys.excepthook = ExceptionHook()
  • crash_on_ipy.py放到项目中,在需要使用该功能的地方import crash_on_ipy

参考

  1. http://www.zhihu.com/question/21572891
阅读此文

2014-12-04
windows.h和winsock2.h冲突

原因

1
2
#include <windows.h>
#include <winsock2.h>
  • 在包含WinSock2.h之前包含了Windows.h
  • Windows.h包含了WinSock.h
  • WinSock.hWinSock2.h不兼容!

解决方案

  • 第一种(优先使用)
1
2
#include <winsock2.h>
#include <windows.h>
  • 第二种
1
2
3
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
阅读此文

2014-12-03
python文件删除

代码

  • os.remove()

  • shutil.re

参考

  1. https://docs.python.org/2/library/os.html#os.remove
阅读此文

2014-12-03
python文件重命名

阅读此文

2014-12-03
python-cgi读取表单数据

代码

1
2
3
4
5
6
7
form = cgi.FieldStorage()
if "name" not in form or "addr" not in form:
print "<H1>Error</H1>"
print "Please fill in the name and addr fields."
return
print "<p>name:", form["name"].value
print "<p>addr:", form["addr"].value

参考

  1. https://docs.python.org/2/library/cgi.html
阅读此文