前面一篇文章介紹了基於golang實現的arp協議,現在來介紹一下python實現的arp協議
代碼如下:
# -*- coding: utf-8 -*-
import socket
import time
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.SOCK_RAW)
s.bind(("enp3s0", socket.SOCK_RAW))
src_addr = "\x50\x3d\xe5\x0e\x35\x3f"
dst_addr = "\xff\xff\xff\xff\xff\xff"
ethertype = "\x08\x06"
while True:
frame = dst_addr + src_addr + ethertype + "\x00\x01" + "\x08\x00" + "\x06" + "\x04" + "\x00\x01" + \
src_addr + "\xc0\xa8\x01\x01" + "\x00\x00\x00\x00\x00\x00" + "\xc0\xa8\x01\x03"
print("send start")
s.send(frame)
time.sleep(1)
代碼s.bind(("enp3s0", socket.SOCK_RAW))其中enp3s0為socket綁定的網卡名稱.
frame為要發送的數據幀,數據幀格式如下:
由於時間倉促,採用的是硬編碼方式.
代碼中\x代表十六進制,所需要的數據都是轉化成十六進制來表示.
硬編碼方式其實就是按照arp協議的定義填寫固定的信息,最後通過socket發送出去.
接下來更新的文章會介紹C語言實現的arp協議,對arp協議感興趣的同學可以繼續關注!