从socket.recv与s.recvfrom获取源和目标IP地址

时间:2014-02-11 04:24:48

标签: python sockets

我已经在python中编写了一个数据包捕获工具,我正在使用以下代码:

packet = socket.recv(sipLocatorConfig.NETWORK_TCP_MAX_SIZE)
eth_length = 14
ip_header = packet[eth_length:20+eth_length]          
#now unpack them 
iph = unpack('!BBHHHBBH4s4s' , ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
iph_length = ihl * 4
ttl = iph[5]
protocol = iph[6]
t = iph_length + eth_length           
tcp_header = packet[t:t+20]
tcph = unpack('!HHLLBBHHH',tcp_header)
source_port = tcph[0]
dest_port = tcph[1]
s_addr1 = socket.inet_ntoa(iph[8]);
d_addr1 = socket.inet_ntoa(iph[9]);

我收到以下错误:

'_socketobject' object has no attribute 'inet_ntoa'

如何获取捕获数据包的源地址和目标地址? 这适用于packet = s.recvfrom(sipLocatorConfig.NETWORK_MAX_SIZE) 感谢

0 个答案:

没有答案