-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
204 lines (168 loc) · 7.3 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import datetime
import gettext
import os
import socket
import sys
import threading
from socketserver import ThreadingTCPServer, StreamRequestHandler, DatagramRequestHandler, ThreadingUDPServer
import wx
from layout.out.MyFrame import MyFrame
class MainFrame(MyFrame):
def __init__(self, *args, **kwds):
MyFrame.__init__(self, *args, **kwds)
self.serverThread = None
self.server = None
self.clientSocket = None
self.clientSocketProtocol = None
def OnAbout(self, event):
wx.MessageBox('About...', '关于', wx.OK)
def OnServerToggle(self, event):
log('debug', "OnServerToggle")
if os.path.isfile('.enable'):
self.OnServerStop()
else:
self.OnServerStart()
def OnServerStart(self):
txtServerPort = self.text_server_port.GetValue()
if not txtServerPort.isdigit():
wx.MessageBox('Invalid server port')
return
elif int(txtServerPort) <= 1024 or int(txtServerPort) >= 65536:
wx.MessageBox('Port must in 1024 to 65535')
return
radP = self.radio_server_protocol
txtProtocol = radP.GetItemLabel(radP.GetSelection())
if txtProtocol == 'TCP':
try:
os.close(os.open('.enable', os.O_CREAT))
self.server = ThreadingTCPServer(('', int(txtServerPort)), self.MainTCPHandler)
self.serverThread = threading.Thread(target=self.server.serve_forever)
self.serverThread.daemon = True
self.serverThread.start()
log('notice', 'TCP Server started')
except OSError as e:
wx.MessageBox(str(e))
log('error', str(e))
except (Exception) as e:
wx.MessageBox('Failed Starting Server')
log('error', str(e))
elif txtProtocol == 'UDP':
try:
os.close(os.open('.enable', os.O_CREAT))
self.server = ThreadingUDPServer(('', int(txtServerPort)), self.MainUDPHandler)
self.serverThread = threading.Thread(target=self.server.serve_forever)
self.serverThread.daemon = True
self.serverThread.start()
log('notice', 'UDP Server started')
except OSError as e:
wx.MessageBox(e)
log('error', e)
except Exception as e:
wx.MessageBox('Failed Starting Server')
log('error', e)
self.btn_server_toggle.SetLabel('停止')
def OnServerStop(self):
log('debug', "OnServerStop")
os.remove('.enable')
self.server.shutdown()
self.server.server_close()
self.btn_server_toggle.SetLabel('启动')
log('notice', 'Server stopped.')
def OnClientToggle(self, event):
log('debug', 'OnClientToggle')
if self.clientSocket:
self.OnClientStop()
else:
self.OnClientStart()
def OnClientStart(self):
log('debug', 'OnClientStart')
txtAddress = self.text_client_address.GetValue()
txtPort = self.text_client_port.GetValue()
if not self.is_valid_ip(txtAddress):
wx.MessageBox('Invalid IP Address!')
return
if not txtPort.isdigit() or int(txtPort) <= 0 or int(txtPort) >= 65536:
wx.MessageBox('Invalid port!')
return
PORT = (txtAddress, int(txtPort))
radP = self.radio_client_protocol
txtProtocol = radP.GetItemLabel(radP.GetSelection())
if txtProtocol == 'TCP':
try:
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clientSocket.connect(PORT)
log('notice', "TCP Connection " + str(self.clientSocket.getsockname()) + " established")
except Exception as e:
log('warning', str(e))
self.clientSocket = None
return
else: # UDP
try:
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
log('notice', "UDP Socket " + str(self.clientSocket) + " established")
except Exception as e:
log('warning', str(e))
self.clientSocket = None
return
self.clientSocketProtocol = txtProtocol
self.btn_client_toggle.SetLabel('关闭Socket')
def OnClientStop(self):
log('debug', 'OnClientStop')
log('notice', "Closing Connection " + str(self.clientSocket))
self.clientSocket.close()
self.clientSocket = None
self.clientSocketProtocol = None
self.btn_client_toggle.SetLabel('建立Socket')
def OnClientSend(self, event):
msg = self.text_client_input.GetValue().encode()
try:
if self.clientSocketProtocol == 'TCP':
self.clientSocket.send(msg)
else: # UDP
txtAddress = self.text_client_address.GetValue()
txtPort = self.text_client_port.GetValue()
PORT = (txtAddress, int(txtPort))
self.clientSocket.sendto(msg, PORT)
self.text_client_input.Clear()
except Exception as e:
log('warning', str(e))
self.OnClientStop()
def is_valid_ip(self, addr):
try:
socket.inet_aton(addr)
return True
except socket.error:
return False
class MainTCPHandler(StreamRequestHandler):
def __init__(self, request, client_address, server):
StreamRequestHandler.__init__(self, request, client_address, server)
def handle(self):
log('notice', "New connection from " + str(self.client_address))
while os.path.isfile('.enable'):
msg = self.request.recv(8192)
if not msg:
log('notice', ("Disconnected from " + str(self.client_address)))
break
log('info', (str(self.client_address) + " " + str(msg)))
class MainUDPHandler(DatagramRequestHandler):
def __init__(self, request, client_address, server):
DatagramRequestHandler.__init__(self, request, client_address, server)
def handle(self):
msg, sock = self.request
log('info', (str(self.client_address) + " " + str(msg)))
if __name__ == "__main__":
if os.path.isfile('.enable'):
os.remove('.enable')
gettext.install("wxPycket") # replace with the appropriate catalog name
wxPycket = wx.App()
frame = MainFrame(None, wx.ID_ANY, "")
def log(type, msg, frame=frame):
label = {'error': '[ERR!]', 'warning': '[WARN]', 'notice': '[NOTI]', 'info': '[INFO]', 'debug': '[DBG ]'}
print(datetime.datetime.now(), label[type], msg, sep=' ', file=sys.stderr)
if not type == 'debug':
frame.text_log.AppendText(str(datetime.datetime.now()) + ' ' + label[type] + ' ' + msg + '\n')
if type in ['err', 'warning', 'notice']:
frame.frame_statusbar.SetStatusText(str(msg))
wxPycket.SetTopWindow(frame)
frame.Show()
wxPycket.MainLoop()