In general, C/C++ can be used to extend the functionality of Python with almost the highest performance you demand. To write a Python extension in C/C++ is relatively easy.
I'll show a simplified extension which is used in real life. This extension is made to extract records in a special file format, .pcap
, and .pcap
file is used to store the captured network packets so that the network activities can be analysed later.
Although there are many alternatives, they cannot achieve the goal in reasonable time. One of these alternatives is scapy
, please don't get me wrong, scapy
is a fabulous networking package. It can automatically parse all the records in .pcap
file, which is an amazing feature. However, the parsing work will also take significant amount of time, especially for a large .pcap
file with hundreds of thousands records inside.
At that time, my goal was quite straightforward. The time
when captured the packet, from which source IP
the packet was sent, and the destination IP
of the packet. Given these demanding, there is no need to parse any record as deep as scapy
would do. I can just check whether it contains IP
layer or not, and if yes, extract the source IP
and destination IP
. Otherwise I'll skip to next record. And that's all.
I decided to name the extension as streampcap
. And the class name would be StreamPcap
so that I can write my Python code as below.
from streampcap import StreamPcap
pcap = StreamPcap("sample.pcap")
packet = pcap.next()
while packet is not None:
print("{} {} {}".format(packet["time"], packet["ip_src"], packet["ip_dst"]))
packet = pcap.next()
In order to implement this functionality, python-dev
should be installed if the OS is Ubuntu/Debian/CentOS and etc Linux based operating systems. As for macOS, personally I use miniconda to manage the Python environment, and I think that miniconda will automatically get the same thing done. And miniconda is also available for Linux based OS. Life is easier!
Continue reading Using C/C++ for Python Extension →