2011年4月24日星期日

[Python] pypcap for python 2.7@win32.xp

pypcap 是pcap在python上的一个封装,但官方网站上的版本只支持python2.5,家里公司机器上的版本都是2.7。在网上搜了一下,没发现特别完整的step by step guide,用mingw自己编了一个,整理记录一下:

I. Package Download
WinPcap installer:         http://www.winpcap.org/archive/
WinPcap WpdPack:       http://www.winpcap.org/archive/
Mingw:                        http://www.mingw.org/
pycap:                         http://code.google.com/p/pypcap/ 
sendpkt:                      http://code.google.com/p/sendpkt/ 
dpkt:                           http://code.google.com/p/dpkt/ 
pexports:                     http://www.emmestech.com/moron_guides/moron1.html

II. Compile
1> Install Mingw
     <> : install
     <> : under ..\bin, change mingw32-make.exe to make.exe
     <> : add into env variable
     <> : check with g++

2> Install winpcap
     <> : install

3> Generat libpython27.a (for mingw usage)
     <> : goto <Python Home>\libs
     <> : run pexports %dll_name% >%def_file%
            Sample : pexports.exe c:\WINDOWS\system32\python27.dll >python27.def
     <> : run dlltool -d %def_file% -l %library% (please do not forget that gcc linker expects library files to have "lib" prefix, e.g. libpython20.a for python20.dll)
            Sample : dlltool -d python27.def -l libpython27.a
4> Compile pypcap
     <> : unzip both pypcap and wdpack into same folder (pypcap 和 wdpack为同级目录 )
     <> : create setup.cfg as below
  1. [build]  
  2. compiler=mingw32  
     <> : python setup.py config
     <> : python setup.py build
     <> : python setup.py install

5> install senddpkt with similiar process
6> verification

     under cmdlie, type python:

  1. >>> import pcap
  2. >>>

III. FAQ

1> ERROR : "couldn't find pcap build or installation directory"
A:    目录结构:
base\
          pypcap
         WpdPack

2>  ERROR : Undefined reference to `_imp___Py_NoneStruct
A:     缺少python2x.a文件,check II. 3