-
Notifications
You must be signed in to change notification settings - Fork 2
/
netconsole.py
103 lines (86 loc) · 2.39 KB
/
netconsole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# Copyright (c) Robert Blair Mason Jr. (rbmj) [email protected]
# see LICENSE for license information.
import socket
import sys
import threading
import atexit
import time
import os
#allow import in both python 2.x and 3.x
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty
#ports
UDP_IN_PORT=6666
UDP_OUT_PORT=6668
#set up recieving socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind( ('',UDP_IN_PORT) )
#set up sending socket - use separate socket to avoid race condition
out = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
out.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
out.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
out.bind( ('',UDP_OUT_PORT) ) #bind is necessary for escoteric reasons stated on interwebs
#set up atexit handler to close sockets
def atexit_func():
sock.close()
out.close()
atexit.register(atexit_func)
#set up threads to emulate non-blocking io
#thread-level emulation required for compatibility with windows
stdin_queue = Queue()
sock_queue = Queue()
def enqueue_output_file(f, q):
for line in iter(f.readline, b''): #thanks to stackoverflow
q.put(line)
def enqueue_output_sock(s, q):
while True:
q.put(s.recv(4096))
stdin_reader = threading.Thread(target = enqueue_output_file, args = (sys.stdin, stdin_queue))
sock_reader = threading.Thread(target = enqueue_output_sock, args = (sock, sock_queue))
stdin_reader.daemon = True
sock_reader.daemon = True
stdin_reader.start()
sock_reader.start()
#send a message out the socket
def send_msg(msg):
out.sendto(line, ('255.255.255.255', UDP_OUT_PORT))
#main loop
have_now=''
group=[]
try:
while True:
try: msg = sock_queue.get_nowait()
except Empty:
pass # no output
else:
sp=msg.split('\n')
assert len(sp)
sp[0]=have_now+sp[0]
have_now=sp[-1]
sp=sp[:-1]
for elem in sp:
if 'Robot_input' in elem:
os.system('clear')
for ele in group:
print ele
group=[]
group.append(elem)
#for i,elem in enumerate(sp):
# if 'in:' in elem:
# os.system('clear')
# sys.stdout.write(elem)
# if i+1!=len(sp):
# sys.stdout.write('\n')
#sys.stdout.write(msg)
try: line = stdin_queue.get_nowait()
except Empty:
pass # no input
else:
send_msg(line)
#time.sleep(0.05)
except:
for elem in group: print elem