sockets - Python, wait until network interface is up? -
i have python script opens port , listens on raspberry. added /etc/rc.local
, working properly. problem socket not created when process being executed.
s = socket.socket() s.bind((host_ip,port)) #e.g. host_ip='192.168.1.32' , port=12345 s.listen(5) while true: c,addr = s.accept() c.send('ack') c.close()
the above code not executed because there no 'eth0' connection available. should do? should loop scan socket status until connection being available? there other more sophisticated solution?
like this:
import urllib2,thread time import sleep import netifaces class _check: def __init__(self): self.uri="http://www.google.com" self.period = 5 self.status = false self.ifaces() def check(self): try: answ = urllib2.urlopen(self.uri) if answ: self.status = true #now can run awesome code ! print "okay go take beer" except exception,e : print e def timer(self,pass_arg) : while true : if self.status != true : self.check() sleep(self.period) print "running" elif self.status == true : print "thread ending" break def ifaces(self): in netifaces.interfaces() : try: print i,netifaces.ifaddresses(i)[2] except: print i, "iface not !" check = _check() thread.start_new_thread(check.timer,(none,))
so answer equal comment answer.
Comments
Post a Comment