forked from qix/penny-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmokoo.py
32 lines (27 loc) · 826 Bytes
/
smokoo.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
import os,re,time,select,fcntl,datetime,random,sys,cgi,BaseHTTPServer,random
from subprocess import PIPE,Popen
class smokoo:
def __init__(self,interface='eth0'):
# Start monitoring
self.ng = os.popen('sudo ngrep -d '+interface) #+' port 80')
self.lastBid = {}
fd = self.ng.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
def read(self):
if select.select([self.ng], [], [], 0) == ([self.ng], [], []):
r = self.ng.read()
r = r.replace("\n", '').replace("\t", '').replace(' ','')
return r
else: return None
def latest(self):
r = self.read()
if r is None: return {}
try:
results = {}
for groups in re.findall('\\^!%[0-9]*\\|([0-9.]+)\\|', r):
(left,) = groups
results[item] = float(left)
return results
except:
return {}