pythonwindowswinpcap

winpcapy errors when moving from python 3.5.2 to python 3.7.4


I've been happily using some python to generate raw Ethernet frames and sending them out an interface of choice on Windows 7 using Python 3.5.2 with winpcapy. I've just installed Python 3.7.4 and now receive an error when trying to run the same code.

import ctypes
import winpcapy
import sys

errbuf = ctypes.create_string_buffer(winpcapy.PCAP_ERRBUF_SIZE)

alldevs = ctypes.POINTER(winpcapy.pcap_if_t)()
winpcapy.pcap_findalldevs(ctypes.byref(alldevs), errbuf)
device = alldevs.contents
interfaces = []
while True:
    interfaces.append(device)
    if not device.next:
        break
    device = device.next.contents
print(interfaces)

The code snippet when run in 3.5 prints a list of the interfaces found. However if I run it in 3.7 I get:

Traceback (most recent call last): File "Z:\proj\eth\socket.py", line 5, in 
<module> errbuf = ctypes.create_string_buffer(winpcapy.PCAP_ERRBUF_SIZE) 
AttributeError: module 'winpcapy' has no attribute 'PCAP_ERRBUF_SIZE' 

Now, I could replace 'PCAP_ERRBUF_SIZE' with an integer value but then I get further issues which looks like there's something generally wrong with how i'm using winpcapy in 3.7. Has anyone else hit these sort of issues?


Solution

  • How did you obtain winpcapy?

    The PyPI winpcapy package is not the same as the historical winpcapy script that was on Google Code. The PyPI package exposes the libpcap API in the winpcapy.winpcapy_types module.

    In the original winpcapy script PCAP_ERRBUF_SIZE was a value defined in the winpcapy module. You could import it just like in the code you provided:

    import winpcapy
    print(winpcapy.PCAP_ERRBUF_SIZE)
    

    However, in the newer PyPI package PCAP_ERRBUF_SIZE is defined in winpcapy.winpcapy_types. So you would need to do something like:

    from winpcapy import winpcapy_types as wtypes
    print(wtypes.PCAP_ERRBUF_SIZE)