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
|
import utils
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
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)
|
sock = socket(AF_PACKET, SOCK_RAW)
|
||||||
|
|
||||||
encoded = packet.message.encode()
|
|
||||||
sock.bind((utils.get_device_name(), 0))
|
sock.bind((utils.get_device_name(), 0))
|
||||||
|
|
||||||
"""
|
frame = utils.broadcast_address + utils.mac_address + b"\x60\x00" + "ZeusWPI is de max!".encode()
|
||||||
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:
|
while True:
|
||||||
send_packet(Frame(destination=utils.broadcast_address,
|
dt_string = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
||||||
protocol=b"\x60\x00",
|
print("Sending message")
|
||||||
message="ZeusWPI is de max!"))
|
sock.send(frame)
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
|
Loading…
Reference in a new issue