|
|
|
|
@ -8,50 +8,50 @@ from Queue import Queue, Empty |
|
|
|
|
|
|
|
|
|
MAGIC_PACKET = "" |
|
|
|
|
for i in xrange(50): |
|
|
|
|
MAGIC_PACKET += chr(random.randrange(ord('a'),ord('z'))) |
|
|
|
|
MAGIC_PACKET += chr(random.randrange(ord('a'),ord('z'))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def split_args(txt): |
|
|
|
|
SA_S0, SA_QUOT = range(2) |
|
|
|
|
split_args_state = SA_S0 |
|
|
|
|
# FSM: |
|
|
|
|
# |
|
|
|
|
# else |
|
|
|
|
# ,--. |
|
|
|
|
# | V " |
|
|
|
|
# `-(S0)----->(QUOT) |
|
|
|
|
# ^ | |
|
|
|
|
# `---------´ |
|
|
|
|
# " |
|
|
|
|
# |
|
|
|
|
args = [] |
|
|
|
|
current = u"" |
|
|
|
|
for i in xrange(len(txt)): |
|
|
|
|
c = txt[i] |
|
|
|
|
if split_args_state==SA_S0: |
|
|
|
|
if c=='"': |
|
|
|
|
split_args_state = SA_QUOT |
|
|
|
|
if len(current)!=0: |
|
|
|
|
args.append(current) |
|
|
|
|
current = u"" |
|
|
|
|
elif c in " \t\n": |
|
|
|
|
if len(current)!=0: |
|
|
|
|
args.append(current) |
|
|
|
|
current = u"" |
|
|
|
|
else: |
|
|
|
|
current += c |
|
|
|
|
elif split_args_state==SA_QUOT: |
|
|
|
|
if c=='"': |
|
|
|
|
split_args_state = SA_S0 |
|
|
|
|
else: |
|
|
|
|
current += c |
|
|
|
|
else: |
|
|
|
|
assert False |
|
|
|
|
if len(current)!=0: |
|
|
|
|
args.append(current) |
|
|
|
|
return args |
|
|
|
|
|
|
|
|
|
SA_S0, SA_QUOT = range(2) |
|
|
|
|
split_args_state = SA_S0 |
|
|
|
|
# FSM: |
|
|
|
|
# |
|
|
|
|
# else |
|
|
|
|
# ,--. |
|
|
|
|
# | V " |
|
|
|
|
# `-(S0)----->(QUOT) |
|
|
|
|
# ^ | |
|
|
|
|
# `---------´ |
|
|
|
|
# " |
|
|
|
|
# |
|
|
|
|
args = [] |
|
|
|
|
current = u"" |
|
|
|
|
for i in xrange(len(txt)): |
|
|
|
|
c = txt[i] |
|
|
|
|
if split_args_state==SA_S0: |
|
|
|
|
if c=='"': |
|
|
|
|
split_args_state = SA_QUOT |
|
|
|
|
if len(current)!=0: |
|
|
|
|
args.append(current) |
|
|
|
|
current = u"" |
|
|
|
|
elif c in " \t\n": |
|
|
|
|
if len(current)!=0: |
|
|
|
|
args.append(current) |
|
|
|
|
current = u"" |
|
|
|
|
else: |
|
|
|
|
current += c |
|
|
|
|
elif split_args_state==SA_QUOT: |
|
|
|
|
if c=='"': |
|
|
|
|
split_args_state = SA_S0 |
|
|
|
|
else: |
|
|
|
|
current += c |
|
|
|
|
else: |
|
|
|
|
assert False |
|
|
|
|
if len(current)!=0: |
|
|
|
|
args.append(current) |
|
|
|
|
return args |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_local_ip(): |
|
|
|
|
def udp_listening_server(): |
|
|
|
|
@ -60,15 +60,15 @@ def get_local_ip(): |
|
|
|
|
s.setblocking(0) |
|
|
|
|
while True: |
|
|
|
|
result = select.select([s],[],[],10) |
|
|
|
|
if len(result[0])==0 : |
|
|
|
|
print "Warning using default local ip" |
|
|
|
|
queue.put([config.LOCAL_IP]) |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
if len(result[0])==0 : |
|
|
|
|
print "Warning using default local ip" |
|
|
|
|
queue.put([config.LOCAL_IP]) |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
msg, address = result[0][0].recvfrom(1024) |
|
|
|
|
msg = unicode(msg) |
|
|
|
|
if msg == MAGIC_PACKET: |
|
|
|
|
queue.put(address) |
|
|
|
|
queue.put(address) |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
queue = Queue() |
|
|
|
|
|