-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_old.py
executable file
·57 lines (45 loc) · 1.62 KB
/
server_old.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import sys
import argparse
import socketserver
parser = argparse.ArgumentParser(
description='Simple lightweight UDP server')
parser.add_argument('--port',
default=10000,
help="UDP port to be listened")
exchange = {}
class UDPHandler(socketserver.BaseRequestHandler):
"""
This class works similar to the TCP handler class, except that
self.request consists of a pair of data and client socket, and since
there is no connection the client address must be given explicitly
when sending data back via sendto().
"""
def handle(self):
data = self.request[0].strip()
# socket = self.request[1]
print("{} wrote:".format(self.client_address[0]))
print(data)
# print(exchange)
# socket.sendto(data.upper(), self.client_address)
class ThreadedUDPServer(socketserver.ForkingMixIn, socketserver.UDPServer):
daemon_threads = True
allow_reuse_address = True
def __init__(self, server_address, RequestHandlerClass):
socketserver.TCPServer.__init__(self, server_address,
RequestHandlerClass)
if __name__ == '__main__':
args = parser.parse_args()
HOST, PORT = '0.0.0.0', int(args.port)
server = ThreadedUDPServer((HOST, PORT), UDPHandler)
print("Now listening on %s:%d" % (HOST, PORT))
print("Press Ctrl+C to Stop")
while True:
try:
server.handle_request()
except KeyboardInterrupt:
server.server_close()
break