2011-09-23 13 views
7

मैं ctypes का उपयोग करके python3 में libpcap का उपयोग करने का प्रयास कर रहा हूं।ctypes और फ़ंक्शन के संदर्भ में

सी में निम्नलिखित समारोह

pcap_lookupnet(dev, &net, &mask, errbuf) 

अजगर में मैं निम्नलिखित है

pcap_lookupnet = pcap.pcap_lookupnet 

mask = ctypes.c_uint32 
net = ctypes.c_int32 

if(pcap_lookupnet(dev,net,mask,errbuf) == -1): 
print("Error could not get netmask for device {0}".format(errbuf)) 
sys.exit(0) 

और त्रुटि मैं मिल

File "./libpcap.py", line 63, in <module> 
if(pcap_lookupnet(dev,net,mask,errbuf) == -1): 
ctypes.ArgumentError: argument 2: <class 'TypeError'>: Don't know how to convert parameter 2 

है आप & blah से कैसे निपटते हैं दिया मूल्य?

उत्तर

13

आपको net और mask के लिए उदाहरण बनाने की आवश्यकता है, और उन्हें पास करने के लिए byref का उपयोग करें।

mask = ctypes.c_uint32() 
net = ctypes.c_int32() 
pcap_lookupnet(dev, ctypes.byref(net), ctypes.byref(mask), errbuf) 
+0

स्टैकओवरफ्लो "धन्यवाद" जैसी टिप्पणियों का उपयोग करने से बचने के लिए कहता है लेकिन मैंने इस मुद्दे के समाधान को हल करने में काफी समय लगाया। इसलिए आपका धन्यवाद! :) – LuckyLuc

1

आप शायद ctypes.pointer उपयोग करने के लिए, इस तरह की जरूरत है:

pcap_lookupnet(dev, ctypes.pointer(net), ctypes.pointer(mask), errbuf) 

अधिक जानकारी के लिए ctypes देखें pointers पर ट्यूटोरियल अनुभाग।

मुझे लगता है कि आपने अन्य तर्कों के लिए ctypes प्रॉक्सी बनाई है। यदि dev को एक स्ट्रिंग की आवश्यकता है, उदाहरण के लिए, आप बस पाइथन स्ट्रिंग में नहीं जा सकते हैं; आपको ctypes_wchar_p या उन पंक्तियों के साथ कुछ बनाने की आवश्यकता है।

+0

शुद्ध और मुखौटा वास्तव में विशिष्ट प्रकार के नीचे देखें पूर्णांक pcap_lookupnet (स्थिरांक चार * डिवाइस, bpf_u_int32 * netp, bpf_u_int32 * maskp, चार * errbuf) कर रहे हैं; – user961346

+0

bpf_u_int32 वास्तव में है ... typedef u_int bpf_u_int32; – user961346

1

ctypes.c_uint32टाइप है।

mask = ctypes.c_uint32() 
net = ctypes.c_int32() 

फिर ctypes.byref का उपयोग कर पारित:

pcap_lookupnet(dev,ctypes.byref(mask),ctypes.byref(net),errbuf) 

आप mask.value का उपयोग कर मान प्राप्त कर सकता है आप एक उदाहरण की जरूरत है।

संबंधित मुद्दे