我的系统是window7、python2.7,pycap和sendpkt结合起来做网络抓包分析非常好,可惜google code上提供的都是针对python2.5的,我的是python2.7,只好自己找例子去编译。

编译采用mingw编译,可以到 http://www.mingw.org/上去下载,下载后安装然后添加安装路径到环境变量,看到如下效果即可:

Java代码
  1. C:\>g++ --help   
  2. Usage: g++ [options] file...   
  3. Options:   
  4.   -pass-exit-codes         Exit with highest error code from a phase   
  5.   --help                   Display this information         
  1. C:\>  
C:\>g++ --help
Usage: g++ [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  (Use '-v --help' to display command line options of sub-processes)
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
C:\>


现在以编译 pycap为例:

1.首先下载pycap http://pypcap.googlecode.com/files/pypcap-1.1.tar.gz
2.下载wdp http://www.winpcap.org/install/bin/WpdPack_4_0_2.zip
3.解压这两个文件到同一目录,注意:要到同一目录
4.在解压后的pycap文件夹中建立一个setup.cfg文件,内容为:

Java代码
  1. [build]   
  2. compiler=mingw32  
[build]
compiler=mingw32

5编译,如下:
Java代码
  1.   
  2. C:\Users\zhaolei\Desktop\pypcap-1.1>python setup.py build   
  3. running build   
  4. running build_ext   
  5. building 'pcap' extension   
  6. creating build   
  7. creating build\temp.win32-2.6  
  8. creating build\temp.win32-2.6\Release   
  9. D:\program\c++\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -I..\WpdPack\includ   
  10.  -ID:\program\python\python2.6\include -ID:\program\python\python2.6\PC -c pcap   
  11. c -o build\temp.win32-2.6\Release\pcap.o -DWIN32 -DWPCAP   
  12. writing build\temp.win32-2.6\Release\pcap.def   
  13. creating build\lib.win32-2.6  
  14. D:\program\c++\MinGW\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.6\Re   
  15. ease\pcap.o build\temp.win32-2.6\Release\pcap_ex.o build\temp.win32-2.6\Release   
  16. pcap.def -L..\WpdPack\lib -LD:\program\python\python2.6\libs -LD:\program\pytho   
  17. \python2.6\PCbuild -lwpcap -liphlpapi -lpython26 -lmsvcr90 -o build\lib.win32-2  
  18. 6\pcap.pyd   
  19.   
  20. C:\Users\zhaolei\Desktop\pypcap-1.1>python setup.py install  
C:\Users\zhaolei\Desktop\pypcap-1.1>python setup.py build
running build
running build_ext
building 'pcap' extension
creating build
creating build\temp.win32-2.6
creating build\temp.win32-2.6\Release
D:\program\c++\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -I..\WpdPack\includ
 -ID:\program\python\python2.6\include -ID:\program\python\python2.6\PC -c pcap
c -o build\temp.win32-2.6\Release\pcap.o -DWIN32 -DWPCAP
writing build\temp.win32-2.6\Release\pcap.def
creating build\lib.win32-2.6
D:\program\c++\MinGW\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.6\Re
ease\pcap.o build\temp.win32-2.6\Release\pcap_ex.o build\temp.win32-2.6\Release
pcap.def -L..\WpdPack\lib -LD:\program\python\python2.6\libs -LD:\program\pytho
\python2.6\PCbuild -lwpcap -liphlpapi -lpython26 -lmsvcr90 -o build\lib.win32-2
6\pcap.pyd
C:\Users\zhaolei\Desktop\pypcap-1.1>python setup.py install


5.利用同样的方法安装sendpkt
6.测试
Java代码 复制代码
  1. C:\Users\zhaolei>python   
  2. Python 2.6 (r26:66721, Oct  2 200811:35:03) [MSC v.1500 32 bit (Intel)] on win   
  3. 32  
  4. Type "help""copyright""credits" or "license" for more information.   
  5. >>> import pcap,sendpkt,dpkt   
  6. >>>  
C:\Users\zhaolei>python
Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pcap,sendpkt,dpkt
>>>



我已经编译好了,懒得自己动手的童鞋就直接下载好了