Sample instrument emulator in Python

#!/usr/bin/env python3
#
# Script to emulate an instrument sending UDP packets to RIC.
#
# If you would like to test the complete path from the aircraft, but the
# instrument is unavailable, use this script.
#
# Change the ip and ports and message ID (they need to match what is in the
# <inst>.ini file). Change the content of the message to whatever is useful
# to you.

# Then run it to write a message to the specified port, which should be sent
# via ric_switch to the proxy on the ground.
#
# This script can also be a reference for instrument code in Python.

import socket
import time


def main():
    udp_id = "INSTID"  # Instrument ID
    # To send from the aircraft to the ground
    udp_send_port = 32106   # Get these ports from
    udp_read_port = 32107   # http://wiki.eol.ucar.edu/sew/Aircraft/UDP-Data.
    udp_ip = "192.168.84.2"  # The IP address of the aircraft server
    # To send from the ground to the aircraft
    udp_send_port = 32107   # Note these are the reverse of above
    udp_read_port = 32106
    udp_ip = "127.0.0.1"

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    sock.bind(("0.0.0.0", udp_read_port))

    while(1):
        buffer = "%s,%s,39.2348,-105.2398,1506.5,1506.4,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0 .0,25.34\r\n" % (udp_id, time.strftime("%Y%m%dT%H%M%S",time.gmtime()))
        print(buffer)

        if sock:
            bytes = sock.sendto(buffer.encode('utf-8'), (udp_ip, udp_send_port))
            #print(bytes)

        time.sleep(1)

if __name__ == "__main__":
    main()