Skip to content

Instantly share code, notes, and snippets.

@ganindu7
Created April 20, 2021 16:41
Show Gist options
  • Save ganindu7/d85dc47a1849c6281f5e65558b2e346f to your computer and use it in GitHub Desktop.
Save ganindu7/d85dc47a1849c6281f5e65558b2e346f to your computer and use it in GitHub Desktop.
async UDP client on python 3.6.9
import socket
import json
import asyncore
UDP_IP = '127.0.0.1'
UDP_PORT = 2000
class AsyncUDPClient(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
self.bind((host, port))
print("connecting.. host = '{0}'' port = '{1}'" .format(host, str(port)))
def handle_connect(self):
print("connected")
def handle_read(self):
data = self.recv(1024)
y = json.loads(data)
print("PM 2.5 ug/m^3 async : %s "% y['PM25MassPerM3'])
def writable(self):
return False;
client = AsyncUDPClient(UDP_IP, UDP_PORT)
asyncore.loop()
@ganindu7
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment