-
Notifications
You must be signed in to change notification settings - Fork 0
/
swockets.py
executable file
·214 lines (174 loc) · 5.18 KB
/
swockets.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
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
"""
swockets by Daniel Swoboda (@snoato, swobo.space)
"""
import socket
import threading
import json
import select
import sys
#Exception for swockets
class SwocketError(IOError):
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
#Class to store necessary client data for swockets in server mode
class SwocketClientSocket():
pass
#Basic Handler as it can be used for swockets
class SwocketHandler(object):
def __init__(self):
self.sock = None
self.connected = None
#if in server mode, sock = SwocketClientSocket of connecting socket
def handshake(self, sock):
return True
#if in server mode, sock = SwocketClientSocket of sending socket
def recv(self, recvObj, sock = None):
print recvObj
#if in server mode, sock = SwocketClientSocket of disconnected socket
def disconnect(self, sock = None):
print "disconnect"
def connect(self, sock):
print "connect"
def handshake_unsuccessful(self):
print "handshake unsuccessful"
#Swockets, the Sw(obo - S)ockets
class swockets:
ISSERVER = 1
ISCLIENT = 2
RUNNING = True
def __init__(self, mode, handler, host=None, port=6666, backlog=1):
self.handler = handler
self.mode = mode
self.handler.sock = self
if mode == swockets.ISSERVER:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('', port))
self.sock.listen(backlog)
self.clients = []
t=threading.Thread(target=self.server_connection_thread, args=())
t.daemon = True
t.start()
elif mode == swockets.ISCLIENT:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
self.client_negotiate()
else:
raise ValueError("You must provide a correct mode")
def __del__(self):
self.stop()
#closes all connections and stops all running loops
def stop(self):
self.RUNNING = False
if self.mode == swockets.ISSERVER:
for client in self.clients:
client.sock.close()
self.sock.close()
#thread that accepts new connections for server
def server_connection_thread(self):
while self.RUNNING:
(clientsocket, address) = self.sock.accept()
self.server_negotiate(clientsocket, address)
#server negotiating function
def server_negotiate(self, clientsocket, address):
if self.handler.handshake(clientsocket):
client = SwocketClientSocket()
client.sock = clientsocket
client.address = address
self.clients.append(client)
self.handler.connect(clientsocket)
t=threading.Thread(target=self.receive_thread, args=(client, client.sock))
t.daemon = True
t.start()
else:
clientsocket.close()
self.handler.handshake_unsuccessful()
#client negotiating function
def client_negotiate(self):
if self.handler.handshake(self.sock):
self.handler.connect(self.sock)
t=threading.Thread(target=self.receive_thread, args=(self.sock, self.sock))
t.daemon = True
t.start()
else:
self.sock.close()
self.handler.handshake_unsuccessful()
#receives one json formatted message between two swockets
def receive_one_message(self, sock, ssock):
try:
recvdStr = ""
recvdMsg = ""
while True:
try:
recvdStr = ssock.recv(1024)
if recvdStr == "":
if self.mode == swockets.ISSERVER:
self.handler.disconnect(sock)
ssock.close()
self.clients.remove(sock)
return None
else:
self.handler.disconnect()
self.sock.close()
return None
else:
recvdMsg+=recvdStr
recvdObj = json.loads(recvdMsg)
recvdMsg = ""
return recvdObj
except ValueError:
pass
except socket.error:
if self.mode == swockets.ISSERVER:
ssock.close()
try:
self.handler.disconnect(sock)
self.clients.remove(sock)
except:
pass
return None
else:
self.handler.disconnect()
self.sock.close()
return None
except UnicodeEncodeError:
return None
#thread that runs in background waiting for messages and forwarding
#them to the handler for further processing by user
def receive_thread(self, sock = None, ssock = None):
if self.mode == swockets.ISCLIENT:
sock = self.sock
ssock = self.sock
while self.RUNNING:
recvdObj = self.receive_one_message(sock, ssock)
if recvdObj == None:
return
self.handler.recv(recvdObj, sock)
sock.sock.close()
#user callable receive function, provide sock if in server mode
def receive(self, sock = None, ssock = None):
if self.mode == swockets.ISCLIENT:
ssock = self.sock
return self.receive_one_message(sock, ssock)
#user callable send function, provide sock if in server mode,
#msg has to be json object
#sock and ssock have to be given in server mode
def send(self, msg, sock = None, ssock = None):
try:
if self.mode == swockets.ISCLIENT:
ssock = self.sock
sendStr = json.dumps(msg)
if sendStr != "":
sendStr += ' ' * (1024-(len(sendStr.encode("utf8")) % 1024))
ssock.sendall(sendStr)
except socket.error:
if self.mode == swockets.ISCLIENT:
self.handler.disconnect()
else:
self.handler.disconnect(sock)
except IndexError:
if sock not in self.clients:
SwocketError("Client not available")