Python help

J.B

J.B

Soldato
Joined
16 Aug 2006
Posts
5,924
Hey guys, hoping a lovely person might be able to help me out. Im not much of a programmer, I can do the PHP and classic ASP type things and have been known to have the odd Java applet compile.

I have this code that I found online that will find a device on my network by broadcasting a set of key packets and respond in a certain way.

Anyway, I after downloading the netiface library I still cant get this to work:

Code:
# digiDiscover.py
# Author: John Kelley  <FirstName> (at) <LastName> dot ca
# 10/24/08

import socket, signal, traceback
from netifaces import interfaces, ifaddresses, AF_INET
from struct import unpack

class SocketTimeOut(Exception):
	def __init__(self, value):
		self.parameter = value
	def __str__(self):
		return repr(self.parameter)

def handler(signum,frame):
	"""This is a handler function called when a SIGALRM is received, 
	it simply raises a string exception"""

	raise SocketTimeOut(frame)

def detectDigiDevice(timeout):
	"""This sends a UDP broadcast packet out over all available
	interfaces with an IPv4 broadcast address. It then detects
	and parses a response from Digi devices such as Connect ME
	and returns the Name/Version, MAC address and IP."""

	listenPort = 1181
	broadcastPort = 2362
	digiDiscoverPacket = "DIGI\x00\x01\x00\x06\xff\xff\xff\xff\xff\xff"
	
	#setup socket
	outsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	outsock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
	outsock.bind(('',listenPort))
	
	#send our discovery packet out over all interfaces
	for ifaceName in interfaces():
		try:
			for i in ifaddresses(ifaceName)[AF_INET]:
				outsock.sendto(digiDiscoverPacket, (i['broadcast'], broadcastPort))
		except:
			True	#for lack of a better way to not care about the exception and just continue
	#wait for a response
	try:
		#setup the timeout
		signal.signal(signal.SIGALRM, handler)
		signal.alarm(timeout)

		#wait for data
		data, addr = outsock.recvfrom(2048)
		
		#disable timeout
		signal.alarm(0);

		#process data
		if not data.startswith('DIGI'):
			return None
		mac = "%02X:%02X:%02X:%02X:%02X:%02X" % (ord(data[10]), ord(data[11]), ord(data[12]), ord(data[13]), ord(data[14]), ord(data[15]))
		len = ord(data[35])
		desc = data[36:(36+len)]+" "
		len2 = ord(data[36+len+7])
		desc += data[36+len+8: 36+len+8+len2]
		return addr[0], mac, desc
	except:
		raise

if __name__ == '__main__':
	try:
		ip, mac, desc = detectDigiDevice(1);
		if (ip == None):
			print "Unable to find a Digi device"
			exit()
		else:
			print "Found '%s' with MAC %s @ %s" % (desc, mac, ip)
	except SocketTimeOut:
		print "Timed out waiting for a response from a Digi device"
	except:
		traceback.print_exc()

It says line 46 "signal.signal(signal.SIGALRM, handler)" module object has no attribute SIGALRM and also says line theres a problem with line 69.

I really dont know what Im doing, is there another library or something I need?

Thanks, its really bugging me.
 
Ok I've read around a bit and from what I can tell the SIGALRM is only available on Mac OS...is that right? Im confuzzled :confused:
 
Back
Top Bottom