elSendNReceive.py

18 months ago

author
Grant
date
Wed Oct 27 11:30:10 2010 -0400
changeset 0
421bc4c29703
permissions
-rw-r--r--

Added elSendNReceive.py, a script to test communication with ethernet link devices. Works some of the time.

     1 #!/usr/bin/python
     3 #Type a command to send it to the entire network
     4 #Commands send to Ethernet Link devices should start with "^^Id"
     5 #Commands sent to the underlying Whozz Calling? whould start with "^^Id-"
     6 from socket import *
     7 import time, threading
     9 class ListenThread (threading.Thread):
    10     def __init__(self):
    11         threading.Thread.__init__(self)
    12         self.alive = False
    13     def run (self):
    14         host = "0.0.0.0"
    15         port = 3520
    16         buffer = 102400
    18         UDPSock = socket(AF_INET,SOCK_DGRAM) #Socket Datagram
    19         UDPSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    20         UDPSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
    21         UDPSock.bind((host,port))
    22         time.clock()
    23         print "-" * 40
    24         print "Starting Server\nType a command, then press enter to broadcast that command\n\n"
    25         self.alive = True
    26         while self.alive:
    27             try:
    28                 data,addr = UDPSock.recvfrom(buffer)
    29                 if not data:
    30                     print "No data."
    31                     break
    32                 elif "quit" in data:
    33                     break
    34                 else:
    35                     donestamp = time.clock()
    37                     if data.rfind("$"):
    38                         data = data[data.rfind("$"):]
    39                         print data
    40             except (KeyboardInterrupt, SystemExit):
    41                 raise
    42                 UDPSock.close()
    43             except:
    44                 traceback.print_exc()
    46     def finish(self):
    47         self.alive = False
    50 Listen = ListenThread()
    51 Listen.start()
    53 host = "255.255.255.255"
    54 port = 3520
    55 UDPSock = socket(AF_INET,SOCK_DGRAM) #Socket Datagram
    56 UDPSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    57 UDPSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
    58 time.sleep(.001)
    59 while 1:
    60     try:
    61         data = raw_input('')
    62         UDPSock.sendto(data,(host,port))
    63     except:
    64         UDPSock.sendto("quit",(host,port))
    65         Listen.finish()
    66         break

mercurial