socket_listen.py

Listens for XBee packets containing the dryer status and updates a corresponding webpage. - Anonymous, 04/30/2012 11:52 am

Download (1.2 kB)

 
   1  # I don't know why but this only works if nc.exe is running, listening on
   2  # port 9750. Then the script works fine.
   3  # nc -L -p 9750
   4  
   5  import socket
   6  import re
   7  
   8  xbeeip = '192.168.1.3'
   9  
  10  # the public network interface
  11  HOST = socket.gethostbyname(socket.gethostname())
  12  
  13  # create a raw socket and bind it to the public interface
  14  s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
  15  s.bind((HOST, 9750))
  16  # 3054 is for IO data
  17  # 9750 is for IP4 packets
  18  
  19  # Include IP headers
  20  s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  21  
  22  # receive all packages
  23  s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  24  
  25  # receive a package
  26  check = True
  27  while(check):
  28      data = s.recvfrom(65565) # Number is buffer size
  29      #if (data[1][0]==xbeeip):
  30      datas = data[0].encode("hex")
  31      if (len(datas) > 80):
  32          status = datas[80:].decode("hex")
  33          status_match = re.match(r"^Dryer",status)
  34          if (status_match):
  35              if (status[-1]=='\x00'):
  36                  status = status[0:-1]
  37              fileout = open('C:/inetpub/wwwroot/dryer1.txt','w')
  38              print status
  39              fileout.write(status)
  40              fileout.close()
  41          
  42  
  43  # disabled promiscuous mode
  44  s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)