datagram_server.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # The basis for this UDP (User Datagram Protocol) portion of this code
3 # came from: http://en.wikipedia.org/wiki/User_Datagram_Protocol (2008-05-01)
4 # This script is a copy of the one used in MINOS, written by Robert Hatcher,
5 # adapted for NOvA.
6 
7 import socket, time, sys
8 
9 PORT = 12345
10 BUFLEN = 512
11 
12 server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
13 server.bind(('', PORT))
14 
15 docontinue = True
16 while docontinue:
17  (message, address) = server.recvfrom(BUFLEN)
18  rtime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
19  rmach = socket.gethostbyaddr(address[0])[0]
20  if ( message == 'SPECIAL_SHUTDOWN_MESSAGE'):
21  sys.stdout.write(rtime)
22  sys.stdout.write(' Received SHUTDOWN message from %s\n'%rmach)
23  docontinue = False
24  else:
25  sys.stdout.write(rtime)
26  sys.stdout.write(' %s : \"%s\"\n'%(rmach, message))
27  sys.stdout.flush()
28 
29 server.close()