-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDP_Echo_Server_test_receive_spines.py
38 lines (27 loc) · 1.13 KB
/
UDP_Echo_Server_test_receive_spines.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
# UDP_Echo_Server.py
import sys, argparse
import socket
RECV_BUFFER_SIZE = 1024
# Get port to listen on as command line argument
def get_args(argv):
parser = argparse.ArgumentParser(description="UDP Echo Server")
parser.add_argument('-p', '--port', required=False, default=12000, type=int)
return parser.parse_args()
def main(argv):
# Process commandline arguments
args = get_args(argv)
# Create a UDP socket (note use of SOCK_DGRAM)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("Opened Server UDP socket")
# Assign IP address and port number to socket
server_socket.bind(('', args.port))
print("Bound socket on port " + str(args.port))
while True:
# Receive the client packet along with the address it is coming from
message, addr = server_socket.recvfrom(RECV_BUFFER_SIZE)
message = message.decode()
print("Received msg from {}:{}\t{}".format(addr[0], addr[1], message))
#Send response back to the address we received from
#server_socket.sendto(message.encode(), addr)
if __name__ == "__main__":
main(sys.argv[1:])