Created
October 9, 2024 09:44
-
-
Save umer0586/1331ac524c525bae7b1c94667ed571de to your computer and use it in GitHub Desktop.
A simple UDP server in python
This file contains 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 threading | |
class UDPServer: | |
def __init__(self,address,buffer_size = 1024): | |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
self.address = address | |
self.buffer_size = buffer_size | |
def setDataCallBack(self,callBack): | |
self.callback = callBack | |
def start(self): | |
self.stop = False | |
self.thread = threading.Thread(target=self.__listen__) | |
self.thread.start() | |
def stop(self): | |
self.sock.close() | |
def __listen__(self): | |
self.sock.bind(self.address) | |
while True: | |
data, address = self.sock.recvfrom(self.buffer_size) | |
self.callback(data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment