def recv(self):
try:
data = super().recv(Message.SIZE)
except OSError as e:
if e.errno == errno.ENETDOWN:
raise BusDown
raise
res = ioctl(self, 0x8906, struct.pack("@LL", 0, 0))
seconds, microseconds = struct.unpack("@LL", res)
timestamp = seconds + microseconds / 1000000
msg = Message.from_bytes(data)
msg.timestamp = timestamp
return msg
python类ENETDOWN的实例源码
def get_listen_ip():
listen_ip = []
sock = None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('8.8.8.8', 53))
listen_ip.append(sock.getsockname()[0])
except:
pass
finally:
if sock:
sock.close()
sock = None
try:
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.connect(('2001:4860:4860::8888', 53))
listen_ip.append(sock.getsockname()[0].partition('%')[0])
except:
pass
finally:
if sock:
sock.close()
if listen_ip:
return listen_ip
else:
import errno
raise OSError(errno.ENETDOWN, '???????')
def main():
global HOST, PORT, EOF, MAXTRIES
global s, tries, loops
try:
s.connect((HOST, PORT))
while True:
# Measure timing using GPIO4
risetime = RCtime(4)
# Send to the connected socket
# (as we're using UDP, we must
# send separate messages)
s.sendall('foo %s%s' % (loops, EOF))
s.sendall('bar %s%s' % (risetime, EOF))
# Advance counter
loops = loops + 1
except socket.error as err:
errcode = err[0]
if errcode==errno.ECONNREFUSED:
print 'Connection refused by host!'
elif errcode==errno.ENETDOWN:
print 'No network connection!'
elif errcode==errno.ENETUNREACH:
print 'Network unreachable!'
elif errcode==errno.ENETRESET:
print 'Network dropped connection!'
elif errcode==errno.ECONNRESET:
print 'Connection reset by peer!'
elif errcode==errno.EHOSTDOWN:
print 'Host is down!'
elif errcode==errno.EHOSTUNREACH:
print 'No route to host!'
else:
print 'Caught: %s!' % err
if tries >= MAXTRIES:
GPIO.cleanup()
s.close()
print 'No connection. Exiting.'
else:
print 'Tried %i of %i times.\nWaiting %is...' % (tries, MAXTRIES, tries/10)
time.sleep(tries/10)
tries = tries + 1
main()
except KeyboardInterrupt:
GPIO.cleanup()
s.close()
print '%i loops' % loops