Created
April 20, 2021 16:41
-
-
Save ganindu7/d85dc47a1849c6281f5e65558b2e346f to your computer and use it in GitHub Desktop.
async UDP client on python 3.6.9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer to a Stackoverflow question