daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python
  2.  
  3. from __future__ import print_function
  4.  
  5. import socket
  6. import struct
  7. import time
  8.  
  9. def Log(*args, **kw):
  10.     print(time.strftime("%H:%M:%S"), *args, **kw)
  11.  
  12. class Monitor(object):
  13.  
  14.     def __init__(self, name="Client", args=(), kwargs={}):
  15.         self.args = args
  16.         self.kwargs = kwargs
  17.  
  18.     def start(self):
  19.         self._run(*(self.args), **(self.kwargs))
  20.  
  21.     def _run(self, *args, **kw):
  22.         group = kw["mgroup"]
  23.         port  = kw["mport"]
  24.  
  25.         Log("mcast group", group, "port", port)
  26.         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  27.         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  28.         sock.bind((group, port))
  29.  
  30.         sock.settimeout(5)
  31.  
  32.         mreq = struct.pack("4sl", socket.inet_aton(group), socket.INADDR_ANY)
  33.         sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
  34.  
  35.         count = 0
  36.         while True:
  37.             try:
  38.                 packet = sock.recv(8192)
  39.                 count += 1
  40.             except socket.timeout:
  41.                 Log("mcast timeout")
  42.             finally:
  43.                 if count > 0 and count % 10 == 0:
  44.                     Log("mcast received", count, "packets")
  45.  
  46. def main(kw):
  47.     client = Monitor(kwargs=kw)
  48.     client.start()
  49.  
  50. if __name__ == "__main__":
  51.     kw = { "mgroup" : "239.1.2.49", "mport" : 20000,
  52.            }
  53.     main(kw)
  54.      
  55. #! /usr/bin/python
  56.  
  57. from __future__ import print_function
  58.  
  59. import socket
  60. import time
  61.  
  62. def_mgroup = "239.1.2.49"
  63. def_mport  = 20000
  64. def_rate   = 2
  65.  
  66. def usage():
  67.     import sys
  68.     print("Usage:", sys.argv[0],
  69.           "[multicast group address [multicast port [rate]]]")
  70.     print()
  71.     print("    multicast group address - default", def_mgroup)
  72.     print("    multicast port          - default", def_mport)
  73.     print("    rate                    - default", def_rate)
  74.  
  75. def main(**kw):
  76.  
  77.     mgroup = kw.get("mgroup", def_mgroup)
  78.     mport  = kw.get("mport",  def_mport)
  79.     rate   = kw.get("rate",   def_rate)
  80.  
  81.     sleepdur = 1.0 / rate               # divide by zero if you ask for it
  82.  
  83.     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  84.     sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 10)
  85.  
  86.     npackets = 0
  87.     while True:
  88.         sock.sendto("robot", (mgroup, mport))
  89.         npackets += 1
  90.         if npackets % 10 == 0: print(time.strftime("%H:%M:%S"), "sent", npackets)
  91.         time.sleep(sleepdur)
  92.  
  93. if __name__ == "__main__":
  94.     args = {}
  95.  
  96.     import sys
  97.     try:
  98.         if len(sys.argv) > 1:
  99.             args["mgroup"] = sys.argv[1]
  100.  
  101.         if len(sys.argv) > 2:
  102.             args["mport"] = int(sys.argv[2])
  103.  
  104.         if len(sys.argv) > 3:
  105.             args["rate"] = int(sys.argv[3])
  106.  
  107.         main(**args)
  108.  
  109.     except Exception as e:
  110.         print(e)
  111.         usage()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top