[Tutor] Re: timer vs. timeout - struct.unpack

S Bobb goluptious at hotmail.com
Mon Aug 9 15:29:02 CEST 2004


Thanks for the help.

I've placed the try/except just as you suggested. Now it's coming back with
another error:

Traceback (most recent call last):
  File "ipinstalludpclient3.py", line 50, in ?
    except socket.error, (errno, strerror):
AttributeError: type object '_socketobject' has no attribute 'error'

The script is still running through properly, it just isn't exiting without
an exception.

I've figured out another roundabout way to extract the MAC address now, so
that I don't have to use dnet.
Does anyone have any other suggestions for creating a UUID? I just read that
commands is Unix-centric. I'd like to be able to use this script
cross-platform.

Sean




>From: Kent Johnson <kent_johnson at skillsoft.com>
>To: "goluptious" <goluptious at hotmail.com>,tutor at python.org
>Subject: Re: [Tutor] Re: timer vs. timeout - struct.unpack
>Date: Mon, 09 Aug 2004 08:29:30 -0400
>
>Sean Bobb,
>
>It looks like maybe the socket module is throwing an exception when the 
>socket read times out. You can use a try / except block to catch the 
>exception and close the socket. For example, something like this:
>
>sock = ... # set up socket
>try:
>   while 1:
>     messin = sock.recv(236)
>     # handle data from socket
>except socket.error, (errno, strerror):
>   sock.close()
>   if errno != 11:
>     raise  # unexpected error, don't hide it
>
>You may have to tweak this a bit, I am guessing at what is going on.
>
>Kent
>
>At 06:31 PM 8/9/2004 +0800, goluptious wrote:
>>I figured out how to extract the data and and display it, but I still
>>haven't managed to figure out a proper method to run my loop.
>>Is there some way to use the Traceback to close the loop so that I can 
>>close
>>the socket?
>>Is a timer a better option? If so, should I use the threading timer or
>>create my own using time?
>>
>>BTW, after a little more searching I found out that the commId was 
>>actually
>>a UUID, so I modified the script to incorporate this also.
>>
>>If anyone has some advice as to how to do all these functions a little 
>>less
>>crudely, or perhaps something that I will be able to port to other 
>>operating
>>systems I would really appreciate it. (I've tried just copying over the
>>dnet.so file from my Linux box to Windows, but it returns an error no 
>>module
>>named dnet, I guess libdnet has to be compiled under Windows.)
>>
>>Thank you,
>>
>>Sean Bobb
>>
>>
>>###--->BEGIN SCRIPT ---###
>>#! /usr/bin/env python
>>
>>from socket import *
>>from struct import *
>>import re
>>import dnet
>>import commands
>>
>>def uuidgen():
>>     return commands.getoutput('uuidgen')
>>
>>servid = '96c363be-b894-43b5-8501-ae5c00e779b3'
>>resv = '\x00'
>>commId = uuidgen()
>>packSize = '\xec\x00'
>>macAddr = '\xff\xff\xff\xff\xff\xff'
>>ipAddr = ''
>>netMask =''
>>gateWay = ''
>>Dns1 = ''
>>Dns2 = ''
>>Dns3 = ''
>>httpPort =''
>>messout = servid + (4 * resv) + commId + (4 * resv) + packSize + (2 * 
>>resv)
>>+ macAddr + (146 * resv)
>>rcvtime = pack('ll', 1l, 0l)
>>
>>sock = socket(AF_INET, SOCK_DGRAM)
>>sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
>>sock.setsockopt(SOL_SOCKET, SO_RCVTIMEO, rcvtime)
>>sock.bind(('<broadcast>',58798))
>>sock.sendto(messout, ('<broadcast>', 58797))
>>while 1:
>>  messin = sock.recv(236)
>>  messunpack = unpack('236s', messin)
>>  spl = re.split('96c36.*?\\xec\\x00\\x00\\x02', messunpack[0])
>>  spl1 = spl[1]
>>  macaddr = dnet.eth_ntoa(spl1[0:6])
>>  ipaddr = inet_ntoa(spl1[8:12])
>>  netmask = inet_ntoa(spl1[12:16])
>>  gateway = inet_ntoa(spl1[16:20])
>>  dns1 = inet_ntoa(spl1[20:24])
>>  dns2 = inet_ntoa(spl1[24:28])
>>  dns3 = inet_ntoa(spl1[28:32])
>>  httpport = str(ord(spl1[32]) * 256 + ord(spl1[33]))
>>  print "MAC Address: " + macaddr + "\nIP Address: " + ipaddr + 
>>"\nNetmask: "
>>+ netmask + "\nGateway: " + gateway +  "\nDNS1: " + dns1 + "\nDNS2: " + 
>>dns2
>>+ "\nDNS3: " + dns3 + "\nHTTP Port: " + httpport + "\n\n"
>>sock.close()
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* 
http://join.msn.com/?page=features/junkmail



More information about the Tutor mailing list