shorten sender
This commit is contained in:
parent
fc6d0a7bb1
commit
1c8bcd2182
1 changed files with 5 additions and 34 deletions
|
@ -4,44 +4,15 @@ from time import sleep
|
|||
|
||||
import utils
|
||||
|
||||
|
||||
class Frame:
|
||||
|
||||
def __init__(self,
|
||||
destination: bytes,
|
||||
protocol: bytes,
|
||||
message: str,
|
||||
source: bytes = utils.mac_address):
|
||||
self.destination: bytes = destination
|
||||
self.source: bytes = source
|
||||
self.protocol: bytes = protocol
|
||||
self.message: str = message
|
||||
|
||||
|
||||
def send_packet(packet: Frame) -> bool:
|
||||
if __name__ == "__main__":
|
||||
sock = socket(AF_PACKET, SOCK_RAW)
|
||||
|
||||
encoded = packet.message.encode()
|
||||
sock.bind((utils.get_device_name(), 0))
|
||||
|
||||
"""
|
||||
The way ethernet works is:
|
||||
- 6 bytes indicating destination mac
|
||||
- 6 bytes indicating source mac
|
||||
- 2 bytes indicating the type
|
||||
- maximum 1500 bytes of data to send
|
||||
"""
|
||||
data = (packet.destination + utils.mac_address +
|
||||
packet.protocol + encoded)
|
||||
dt_string = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
||||
print("Sending message to {} @{}: {}".format(packet.destination, dt_string, packet.message))
|
||||
sock.send(data)
|
||||
return True
|
||||
frame = utils.broadcast_address + utils.mac_address + b"\x60\x00" + "ZeusWPI is de max!".encode()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
while True:
|
||||
send_packet(Frame(destination=utils.broadcast_address,
|
||||
protocol=b"\x60\x00",
|
||||
message="ZeusWPI is de max!"))
|
||||
dt_string = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
||||
print("Sending message")
|
||||
sock.send(frame)
|
||||
sleep(1)
|
||||
|
|
Loading…
Reference in a new issue