-
Notifications
You must be signed in to change notification settings - Fork 0
/
CN_Echo_Server.py
38 lines (20 loc) · 1.2 KB
/
CN_Echo_Server.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
import newSocket as socketLib
class CN_Echo_Server(object):
def __init__(self,IP=socketLib.findByDomain("T3"),port=80):
socket, AF_INET, SOCK_DGRAM, timeout = socketLib.socket, socketLib.AF_INET, socketLib.SOCK_DGRAM, socketLib.timeout
with socket(AF_INET, SOCK_DGRAM) as sock:
sock.bind((IP,port))
sock.settimeout(2.0) # 2 second timeout
print ("UDP Server started on IP Address {}, port {}".format(IP,port))
while True:
try:
bytearray_msg, address = sock.recvfrom(1024)
source_IP, source_port = address
print ("\n{} byte message received from ip address {}, port {}:".format(len(bytearray_msg),source_IP,source_port))
print ("\n"+bytearray_msg.decode("UTF-8"))
lenx= sock.sendto(bytearray_msg, address)
print ("\n{} byte message echoed")
except timeout:
continue
if __name__ == '__main__':
CN_Echo_Server()