user961346
user961346

Reputation: 301

Accessing a ctypes array of data

I wrote a wrapper class around libpcap for python3 but I'm struggling to find useful documentation on how to deal with ctype arrays in python.

self.packetdata = ctypes.POINTER(ctypes.c_ubyte*65536)() 
....
return  self.pkthdrPointer.contents.len,self.packetdata.contents

I am accessing it this way..

#!/usr/bin/python3.2 
from pycap import pycap
packet = pycap(interface="wlan0", count=10)
for len,data in packet:
    print(type(data))

The output is as follows:

<class 'pcap_struct.c_ubyte_Array_65536'>
<class 'pcap_struct.c_ubyte_Array_65536'>
<class 'pcap_struct.c_ubyte_Array_65536'>
<class 'pcap_struct.c_ubyte_Array_65536'>

now the array is initialized to 65536 0's and the packet data is "len" long. I need to convert the data into a more workable format, but I can't make it a string because packets can have null characters. Anyone have a suggestion on what (and how) datatype I should convert it to?

Upvotes: 0

Views: 565

Answers (1)

cecilkorik
cecilkorik

Reputation: 1431

Under Python 3.x, you want the "bytes" or "bytearray" type to represent octet streams like packets. From there, you can manipulate it or convert it into a string if you need to for display. But for the native data type you would most likely want a bytearray I'm thinking.

Upvotes: 1

Related Questions