TypeError handle_arp_packet missing 1 required positional argument ifnamedef handle_arp_replypacket eth_header = packetETH_HEADER_LEN arp_header = packetETH_HEADER_LENETH_HEADER_LEN+ARP_HEADER_L
在调用handle_arp_packet()函数时,需要传入ifname参数,但是在handle_arp_reply()函数中调用handle_arp_packet()时没有传入ifname参数,导致出现TypeError: handle_arp_packet() missing 1 required positional argument: 'ifname'错误。
解决方法是在handle_arp_reply()函数中调用handle_arp_packet()时传入ifname参数,例如:
def handle_arp_reply(packet, ifname): eth_header = packet[:ETH_HEADER_LEN] arp_header = packet[ETH_HEADER_LEN:ETH_HEADER_LEN+ARP_HEADER_LEN] src_mac = ':'.join(['%02x' % b for b in eth_header[6:12]]) src_ip = socket.inet_ntoa(arp_header[8:12]) dst_mac = ':'.join(['%02x' % b for b in arp_header[18:24]]) dst_ip = socket.inet_ntoa(arp_header[20:24]) ip_mac_dict[src_ip] = src_mac ip_mac_dict[dst_ip] = dst_mac handle_arp_packet(packet, ifname) # 添加ifname参数
另外,还需要在listen_arp_packets()函数中调用handle_arp_reply()时传入ifname参数,例如:
def listen_arp_packets(ifname): s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL)) s.bind((ifname, ETH_P_ALL)) while True: packet = s.recv(2048) handle_arp_packet(packet, ifname) # 添加ifname参
原文地址: https://www.cveoy.top/t/topic/fq3M 著作权归作者所有。请勿转载和采集!