Skip to content

Instantly share code, notes, and snippets.

@seym45
Created November 17, 2024 17:01
Show Gist options
  • Save seym45/758b1c4aebc0f2b7d22d348c3e6429da to your computer and use it in GitHub Desktop.
Save seym45/758b1c4aebc0f2b7d22d348c3e6429da to your computer and use it in GitHub Desktop.
import socket
import time
from datetime import datetime
import sys
def create_packet():
# Get current time and date
now = datetime.now()
# Format time as HHMMSS
time_str = now.strftime("%H%M%S")
# Format date as DDMMYY
date_str = now.strftime("%d%m%y")
# Create packet with updated time and date
# Original: *HQ,6170881331,V1,094131,V,2356.9027,N,09022.8075,E,000.29,000,040218,FFFF9FFF,470,02,00217,41677#
packet = (
f"*HQ,6170881331,V1,{time_str},V,2356.9027,N,09022.8075,E,"
f"000.29,000,{date_str},FFFF9FFF,470,02,00217,41677#"
)
return packet.encode()
def send_packet(sock, server_address, packet):
try:
# Send packet
sock.sendto(packet, server_address)
print(f"Packet sent at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Packet content: {packet.decode()}")
except socket.error as e:
print(f"Error sending packet: {e}")
return False
return True
def main():
# Server configuration
SERVER_IP = "5.223.51.136"
SERVER_PORT = 5013
server_address = (SERVER_IP, SERVER_PORT)
# Create UDP socket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print(f"Connected to {SERVER_IP}:{SERVER_PORT}")
except socket.error as e:
print(f"Failed to create socket: {e}")
sys.exit(1)
print("Starting packet transmission (Press Ctrl+C to stop)...")
try:
while True:
# Create and send packet
packet = create_packet()
if not send_packet(sock, server_address, packet):
break
# Wait for 60 seconds
time.sleep(60)
except KeyboardInterrupt:
print("\nPacket transmission stopped by user")
except Exception as e:
print(f"An error occurred: {e}")
finally:
sock.close()
print("Socket closed")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment