Created
December 2, 2016 00:34
-
-
Save arthurafarias/7258a2b83433dfda013f1954aaecd50a to your computer and use it in GitHub Desktop.
UDP Simple Client and Threaded Server written in Python
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 sys | |
HOST, PORT = "localhost", 8888 | |
data = " ".join(sys.argv[1:]) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.sendto(data + "\n", (HOST, PORT)) | |
received = sock.recv(1024) | |
print("Sent: {}".format(data)) | |
print("Received: {}".format(received)) |
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 SocketServer, threading, time | |
class ThreadedUDPRequestHandler(SocketServer.BaseRequestHandler): | |
def handle(self): | |
data = self.request[0].strip() | |
socket = self.request[1] | |
current_thread = threading.current_thread() | |
print("{}: client: {}, wrote: {}".format(current_thread.name, self.client_address, data)) | |
socket.sendto(data.upper(), self.client_address) | |
class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer): | |
pass | |
if __name__ == "__main__": | |
HOST, PORT = "0.0.0.0", 8888 | |
server = ThreadedUDPServer((HOST, PORT), ThreadedUDPRequestHandler) | |
server_thread = threading.Thread(target=server.serve_forever) | |
server_thread.daemon = True | |
try: | |
server_thread.start() | |
print("Server started at {} port {}".format(HOST, PORT)) | |
while True: time.sleep(100) | |
except (KeyboardInterrupt, SystemExit): | |
server.shutdown() | |
server.server_close() | |
exit() |
how about a threaded client that doesn't wait for a response?
This is ass
uff...
In this time I was using gist as pastebin.
Gonna elaborate an example over an udp server and put here.
😅
Well, I remember I did find it helpful at the time. I was getting started on Python.
I don't understand why this guy say "this is ass". This is the first result on google when someone asks for python threaded udp server, maybe it's helpful to people.
Anyway...
You can find a better description on how to make a threaded UDP Server to handle a lot of requests at same time using python here:
Well, I remember I did find it helpful at the time. I was getting started on Python.
Thank you!! =D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
Thanks for this example, it's very helpful. I have a question though, what does this line :
current_thread = threading.current_thread()
exactly do?
The doc says that it :
But apparently in your code it doesn't simply "return the caller's thread of control" (that would be the thread started in your main part), instead it apparently :
I'm very confused as, not only is this behaviour radically different from my understanding of the documentation, but you're using two completely unrelated ways of creating a thread and running it (in the handler and in the main). Could you elaborate a bit? In this state the handler thread seems utterly "magical" to me.