frame-it/python/sender.py
2020-01-21 03:27:35 +01:00

47 lines
1.3 KiB
Python

import datetime
from _socket import socket, AF_PACKET, SOCK_RAW
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:
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
if __name__ == "__main__":
while True:
send_packet(Frame(destination=utils.broadcast_address,
protocol=b"\x60\x00",
message="ZeusWPI is de max!"))
sleep(1)