我对Python3中的包嗅探器有问题。在
python版本:3.4
我遵循了一些有用的教程,但不是在我的电脑上。
这段代码必须获取mac地址,将其转换为字符串,并且在main()方法中应该将目标mac、源mac和协议打印给我。在
代码:嗅探器_演示.py在import socket
import struct
import textwrap
def main():
conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
while True:
raw_data, addr = conn.recvfrom(65536)
# one's and zero's put to the method ehternet_frame
dest_mac, src_mac, eth_proto, data = ethernet_frame(raw_data)
print('\nEthernet Frame:')
print('Destination: {}, Source: {}, Protocol: {}'.format(dest_mac, src_mac, eth_proto))
# Unpack ethernet frame
def ethernet_frame(data):
dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
return get_mac_addr(dest_mac), get_mac_addr(src_mac), socket.htons(proto), data[14:]
# Return properly formatted MAC address: (ie AA:BB:CC:DD:EE:FF)
def get_mac_addr(bytes_addr):
bytes_str = map('{:02x}'.format, bytes_addr)
return ':'.join(bytes_str).upper()
main()
错误是:
^{pr2}$
怎么解决?在