나는 파이썬에서 ctypes를 통해 libpcap을 사용하고 있습니다. 작은 래퍼를 작성 했으므로 pcap 함수를보다 편리하게 액세스 할 수 있습니다. 예 : pcap_geterr는pcap_stat struct를 ctypes로 파이썬의 pcap_stats에 전달하십시오.
# char *pcap_geterr(pcap_t *p);
geterr = _pcap.pcap_geterr
geterr.argtypes = [ctypes.c_void_p]
geterr.restype = ctypes.c_char_p
처럼 보인다하지만 지금은 pcap_stats를 사용할 필요가 내가 함수에 pcap_stat 구조체를 통과하는 데 문제가 있습니다. 나는 통계에 대한 두 번째 인수 유형이 파이썬에서 함수에 C 구조체를 전달하는 방법을 다음 오른쪽 경우 확실하지 않다
class pcap_stat(ctypes.Structure):
'''
struct pcap_stat {
u_int ps_recv; /* number of packets received */
u_int ps_drop; /* number of packets dropped */
u_int ps_ifdrop; /* drops by interface, not yet supported */
#ifdef WIN32
u_int bs_capt; /* number of packets that reach the application */
#endif /* WIN32 */
};
'''
_fields_ = [("ps_recv", ctypes.c_uint), ("ps_drop", ctypes.c_uint), ("ps_ifdrop", ctypes.c_uint)]
# int pcap_stats(pcap_t *p, struct pcap_stat *ps);
stats = _pcap.pcap_stats
stats.argtypes = [ctypes.c_void_p, ctypes.c_void_p] # is the second argument right?
stats.restype = ctypes.c_uint
처럼
코드가 보인다. C에서 그것은 아마도 보일 것입니다
struct pcap_stat stat;
pcap_stats(handle, &stat);
그러나 어떻게 파이썬에서 도대체?
아아 젠장 ... 지금 pcap_stats ... 전에 pcap_close를 주석 처리하지 않은 것을 알기 위해 3 시간 쯤 걸렸습니다. 그 이유는 항상 -1과 "잘못된 파일 설명자"를 반환합니다. 고맙습니다. – Cravid