-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
78 lines (64 loc) · 2.26 KB
/
app.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
__author__ = 'nobit'
import wx
import socket
import threading
import time
myEVT_COUNT = wx.NewEventType()
EVT_COUNT = wx.PyEventBinder(myEVT_COUNT, 1)
#
myEVT_RECV = wx.NewEventType()
EVT_RECV = wx.PyEventBinder(myEVT_RECV, 1)
class CountEvent(wx.PyCommandEvent):
"""Evetn to singal that a count value is ready
"""
def __init__(self, etype, eid, value=None):
wx.PyCommandEvent.__init__(self, etype, eid)
self._value = value
def GetValue(self):
"""Returns the vlaue from the event.
@return: the value of this event
"""
return self._value
class Countingthread(threading.Thread):
def __init__(self, parent, value):
threading.Thread.__init__(self)
self._parent = parent
self._value = value
self.HOST = '0.0.0.0' # Symbolic name meaning all available interfaces
self.PORT = 50007 # Arbitrary non-privileged port
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.bind((self.HOST, self.PORT))
self.conntected = False
self.running = True
def run(self):
while self.running:
time.sleep(1)
evt = CountEvent(myEVT_COUNT, -1, self._value)
wx.PostEvent(self._parent, evt)
if self.conntected == False:
print "Init"
self.s.setblocking(False)
self.s.settimeout(0.001)
try:
self.s.listen(1)
self.conn, self.addr = self.s.accept()
print 'Connected by', self.addr
self.conntected = True
except socket.timeout:
pass
else:
try:
print "try read"
self.msg = self.conn.recv(1024)
except socket.error as m1:
print m1
self.msg = None
pass
if self.msg:
evt = CountEvent(myEVT_RECV, -1, self.msg)
wx.PostEvent(self._parent, evt)
elif self.msg == '':
print "Client Disconnect"
self.conntected = False
def stop(self):
self.running = False