-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_udp.py
30 lines (24 loc) · 935 Bytes
/
main_udp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
import usocket as socket
import config
import machine
from lib import logging
from nce.network.network_connector import NetworkConnector
logging.basic_config(level=logging.INFO)
logger = logging.get_logger("__main__")
if __name__ == '__main__':
message = "Hello, UDP. Can you hear me?".encode()
logger.info("Opening UDP Socket")
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = socket.getaddrinfo(config.UDP_ENDPOINT_ADDRESS, config.UDP_ENDPOINT_PORT)[0][-1]
logger.info("Resolved Address [{}]".format(addr))
logger.info(
"Sending UDP message to {}:{} with body {}".format(
config.UDP_ENDPOINT_ADDRESS,
config.UDP_ENDPOINT_PORT,
message))
s.sendto(message, addr)
logger.info("Sent UDP Message to the UDP Broker")
logger.info("Closing the network connection")
NetworkConnector().disconnect()
machine.idle()