在Windows 10上安装pypcap的步骤,适用于python 2.7 (64位)。

5
我正在尝试从以下链接安装pypcap:https://github.com/dugsong/pypcap/blob/master/INSTALL#L75 我正在尝试在Windows 10 - 64位和Python 2.7.11 64位上安装它。我下载了源代码并将其移动到c:\pypcap。我还下载了pyrex并安装了它。它按照安装页面的说法将wpdpack文件放在了c:\wpdpack中。创建了配置文件并更改了makefile。
我还从他们的网站安装了mingw基本包和g++包。
我尝试执行以下操作:
C:\pypcap> set PATH=%PATH%;c:\MinGW\bin;c:\Python27
C:\pypcap> mingw32-make

在这里它失败了,出现了以下错误:

In file included from C:\Python27\include/Python.h:8:0,
                 from pcap.c:4:
C:\Python27\include/pyconfig.h:68:16: fatal error: io.h: No such file or directory
 #include <io.h>

我没有这个io.h文件。我该怎么做才能编译和使用pypcap

在尝试之前,我尝试过

C:\pypcap>pip install pypcap
Collecting pypcap
  Using cached pypcap-1.1.4.tar.gz
    Complete output from command python setup.py egg_info:
    pcap.h not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in c:\users\USER\appdata\local\temp\pip-build-fvgkhs\pypcap\

之后我尝试使用轮子下载,但它也失败了。

感谢您的帮助。


你是否已经安装了libpcap? - totoro
2个回答

7
  1. 解压缩WinPcap开发包,使得你拥有C:\WpdPack\IncludeC:\WpdPack\Lib

  2. 解压缩pypcap源代码,使得你拥有C:\pypcap-1.1.4\setup.py

  3. 安装Microsoft Visual C++ Compiler for Python 2.7

  4. 从以下路径启动适用于你的Python27位数的cmd提示符:
    开始 -> 程序 -> Microsoft Visual C++ Compiler Package for Python 2.7

  5. 使用以下命令在该cmd提示符中安装/构建pypcap:

    set INCLUDE=%INCLUDE%;c:\WpdPack\Include
    set LIB=%LIB%;c:\WpdPack\Lib
    pushd C:\pypcap-1.1.4
    python setup.py install


根据此链接,WinPcap在Windows 10上存在兼容性问题,建议您改用具有WinPcap兼容性的npcap SDK + npcap安装程序。 - prgDevelop
...然后将生成的文件的扩展名.egg重命名为.zip并解压缩文件。不得不再次搜索此内容。 - Niko

3

修正答案

现在你拥有一个包括头文件在内的完整的MinGW GCC编译器。

io.h应存储在C:\MinGW\include中。

更新

我现在已经成功使用Python 2.7.11进行了编译(在Wine下,因为我不运行Windows,但应该是一样的),按照您在问题中描述的步骤进行操作。

我需要做的一件事是修改

中的setup.py的某些部分,因为它没有设置正确的路径到pcap包含文件。

如果您也这样做,请告诉我。

希望这可以帮助您。

setup.py的更改

使用这个修改后的版本替换_pcap_config函数:

def _pcap_config(self, dirs=[ None ]):
    cfg = {}
    if not dirs[0]:
        dirs = [ '/usr', sys.prefix ] + glob.glob('/opt/libpcap*') + \
               glob.glob('../libpcap*') + glob.glob('../wpdpack*')
    for d in dirs:
        for sd in ('include/pcap', 'include', ''):
            if os.path.exists(os.path.join(d, sd, 'pcap.h')):
                cfg.setdefault('include_dirs', []).append(os.path.join(d, sd))
    for d in dirs:
        for sd in ('include/pcap', 'include', ''):
            for sd in ('lib', 'lib64', ''):
                for lib in (('pcap', 'libpcap.a'),
                            ('pcap', 'libpcap.so'),
                            ('pcap', 'libpcap.dylib'),
                            ('wpcap', 'wpcap.lib')):
                    if os.path.exists(os.path.join(d, sd, lib[1])):
                        cfg['library_dirs'] = [ os.path.join(d, sd) ]
                        cfg['libraries'] = [ lib[0] ]
                        if lib[0] == 'wpcap':
                            cfg['libraries'].append('iphlpapi')
                            cfg['extra_compile_args'] = \
                                [ '-DWIN32', '-DWPCAP' ]
                        print 'found', cfg
                        self._write_config_h(cfg)
                        return cfg
    raise Exception("couldn't find pcap build or installation directory")

它基本上只是将包含pcap.h所有路径添加到包含中。 编译
Z:\pypcap-master>mingw32-make
python setup.py config --with-pcap=c:\\wpdpack
running config
found {'libraries': ['wpcap', 'iphlpapi'], 'library_dirs': ['c:\\wpdpack\\lib'], 'extra_compile_args': ['-DWIN32', '-DWPCAP'], 'include_dirs': ['c:\\wpdpack\\include/pcap', 'c:\\wpdpack\\include']}
python setup.py build
running build
running build_ext
building 'pcap' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
C:\MinGW\bin\gcc.exe -mdll -O -Wall -Ic:\wpdpack\include/pcap -Ic:\wpdpack\include -IC:\Python27\include -IC:\Python27\PC -c pcap.c -o build\temp.win32-2.7\Release\pcap.o -DWIN32 -DWPCAP
pcap.c: In function '__pyx_f_4pcap_3bpf_filter':
pcap.c:328:48: warning: passing argument 2 of 'PyObject_AsCharBuffer' from incompatible pointer type
   __pyx_1 = (PyObject_AsCharBuffer(__pyx_v_buf,(&__pyx_v_p),(&__pyx_v_n)) < 0);
                                                ^
In file included from C:\Python27\include/Python.h:133:0,
                 from pcap.c:4:
C:\Python27\include/abstract.h:476:22: note: expected 'const char **' but argument is of type 'char **'
      PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
                      ^
pcap.c:337:86: warning: pointer targets in passing argument 2 of 'bpf_filter' differ in signedness [-Wpointer-sign]
   __pyx_1 = (bpf_filter(((struct __pyx_obj_4pcap_bpf *)__pyx_v_self)->fcode.bf_insns,__pyx_v_p,__pyx_v_n,__pyx_v_n) == 0);
                                                                                      ^
In file included from pcap.c:30:0:
c:\wpdpack\include/pcap/pcap.h:354:7: note: expected 'const u_char *' but argument is of type 'char *'
 u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int);
       ^
pcap.c: In function '__pyx_f_4pcap_4pcap_dispatch':
pcap.c:955:91: warning: passing argument 3 of 'pcap_dispatch' from incompatible pointer type
   __pyx_v_n = pcap_dispatch(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,__pyx_1,__pyx_f_4pcap___pcap_handler,((unsigned char *)(&__pyx_v_ctx)));
                                                                                           ^
In file included from pcap.c:30:0:
c:\wpdpack\include/pcap/pcap.h:300:5: note: expected 'pcap_handler' but argument is of type 'void (*)(void *, struct pcap_pkthdr *, char *)'
 int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
     ^
pcap.c: In function '__pyx_f_4pcap_4pcap_loop':
pcap.c:1059:99: warning: passing argument 3 of 'pcap_ex_next' from incompatible pointer type
     __pyx_v_n = pcap_ex_next(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,(&__pyx_v_hdr),(&__pyx_v_pkt));
                                                                                                   ^
In file included from pcap.c:31:0:
pcap_ex.h:13:7: note: expected 'u_char **' but argument is of type 'char **'
 int   pcap_ex_next(pcap_t *pcap, struct pcap_pkthdr **hdr, u_char **pkt);
       ^
pcap.c: In function '__pyx_f_4pcap_4pcap_sendpacket':
pcap.c:1154:98: warning: pointer targets in passing argument 2 of 'pcap_sendpacket' differ in signedness [-Wpointer-sign]
   __pyx_3 = PyInt_FromLong(pcap_sendpacket(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,__pyx_1,__pyx_2)); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 337; goto __pyx_L1;}
                                                                                                  ^
In file included from pcap.c:30:0:
c:\wpdpack\include/pcap/pcap.h:311:5: note: expected 'const u_char *' but argument is of type 'char *'
 int pcap_sendpacket(pcap_t *, const u_char *, int);
     ^
pcap.c: In function '__pyx_f_4pcap_4pcap___next__':
pcap.c:1312:99: warning: passing argument 3 of 'pcap_ex_next' from incompatible pointer type
     __pyx_v_n = pcap_ex_next(((struct __pyx_obj_4pcap_pcap *)__pyx_v_self)->__pcap,(&__pyx_v_hdr),(&__pyx_v_pkt));
                                                                                                   ^
In file included from pcap.c:31:0:
pcap_ex.h:13:7: note: expected 'u_char **' but argument is of type 'char **'
 int   pcap_ex_next(pcap_t *pcap, struct pcap_pkthdr **hdr, u_char **pkt);
       ^
C:\MinGW\bin\gcc.exe -mdll -O -Wall -Ic:\wpdpack\include/pcap -Ic:\wpdpack\include -IC:\Python27\include -IC:\Python27\PC -c pcap_ex.c -o build\temp.win32-2.7\Release\pcap_ex.o -DWIN32 -DWPCAP
pcap_ex.c: In function 'pcap_ex_next':
pcap_ex.c:237:34: warning: passing argument 3 of 'pcap_next_ex' from incompatible pointer type
  return (pcap_next_ex(pcap, hdr, pkt));
                                  ^
In file included from pcap_ex.c:16:0:
c:\wpdpack\include/pcap/pcap.h:303:6: note: expected 'const u_char **' but argument is of type 'u_char **'
 int  pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
      ^
writing build\temp.win32-2.7\Release\pcap.def
creating build\lib.win32-2.7
C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-2.7\Release\pcap.o build\temp.win32-2.7\Release\pcap_ex.o build\temp.win32-2.7\Release\pcap.def -Lc:\wpdpack\lib -LC:\Python27\libs -LC:\Python27\PCbuild -LC:\Python27\PC\VS9.0 -lwpcap -liphlpapi -lpython27 -lmsvcr90 -o build\lib.win32-2.7\pcap.pyd

Z:\pypcap-master>

连接

使用MinGW(32位),您需要安装32位Python。使用64位Python,您需要安装MinGW(64位)

因此,您可以安装32位Python或64位MinGW。

我们快到了,您一定能搞定的 :-)


libpcap和winpcap有什么区别吗?我必须安装libpcap吗? - MyNick
WinPcap不会安装你编译pypcap所需的头文件。 - totoro
我下载了libpcap和README.Win32,它指定需要安装winpcap并描述如何进行安装。INSTALL.txt文件说应该使用configure文件,但是在Windows上没有这个文件。我该如何安装它? - MyNick
抱歉,您没有从wpdpack获取头文件。 - totoro
你能在你的系统中找到 io.h 文件吗? - totoro
显示剩余15条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接