-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket3.py
More file actions
182 lines (147 loc) · 4.23 KB
/
websocket3.py
File metadata and controls
182 lines (147 loc) · 4.23 KB
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
from socket import *
import threading
import time
class WebSocketException(Exception):
def __init__(self,message):
self.msg = message
def __str__(self):
return repr(self.msg)
class WebSocket:
def __init__(self,uri):
self.uri = uri #urlparse(uri)
protocol = self.uri.scheme
if not( protocol=='ws' or protocol=='wss' ):
raise WebSocketException('Unsupported protocol: '+protocol)
self.headers = {}
self.handshakecompleted = False
def setHeaders(self,headers):
self.headers = headers
def connect(self):
host = self.uri.hostname
path = self.uri.path
if path == '':
path = '/'
query = self.uri.query
if query != '':
path += query
origin = 'http://' + host
port = self.uri.port
if port != 80:
host += ":" + repr(port)
extraheaders = ''
for k,v in self.headers:
extraheaders += k + ': ' + v + '\r\n'
request = 'GET ' + path + ' HTTP/1.1\r\n'
request += 'Upgrade: WebSocket\r\n'
request += 'Connection: Upgrade\r\n'
request += 'Host: ' + host + '\r\n'
request += 'Origin: ' + origin + '\r\n'
request += extraheaders
request += '\r\n'
self.socket = self.createSocket()
self.sockfile = self.socket.makefile(mode='rwb')
self.sockfile.write(request.encode('utf-8'))
self.sockfile.flush()
header = self.sockfile.readline().strip()
if header != b'HTTP/1.1 101 Web Socket Protocol Handshake':
raise WebSocketException('Invalid handshake response: '+str(header))
header = self.sockfile.readline().strip()
if header != b'Upgrade: WebSocket':
raise WebSocketException('Invalid handshake response: '+str(header))
header = self.sockfile.readline().strip()
if header != b'Connection: Upgrade':
raise WebSocketException('Invalid handshake response: '+str(header))
while True:
header = self.sockfile.readline().strip()
print ('skipping header: ' + str(header))
if len(header) == 0:
break
print ('handshake completed.')
self.handshakecompleted = True
def createSocket(self):
scheme = self.uri.scheme
host = self.uri.hostname
port = self.uri.port
if port == -1:
if scheme == 'ws':
port = 80
elif scheme == 'wss':
port = 443
if scheme == 'ws':
s = socket(AF_INET,SOCK_STREAM)
s.connect( (host,port) )
return s
else:
raise WebSocketException('uops..wss protocol not supported yet :p')
def send(self,message):
if False == self.handshakecompleted:
raise WebSocketException('handshake is not completed yet, cannot send!')
self.sockfile.write(b'\x00')
self.sockfile.write( (message).encode('utf-8') )
self.sockfile.write(b'\xff')
self.sockfile.flush()
def recv(self):
if False == self.handshakecompleted:
raise WebSocketException('handshake is not completed yet, cannot receive!')
ret = bytearray()
b = self.sockfile.read(1)
if (b[0]&0x80) == 0x80:
length = 0
while True:
b = (self.sockfile.read(1)[0]) & 0x7f
length = b * 128 + length
if (b&0x80) == 0x80:
break
print ('reading ' + length + ' bytes..')
garbage = self.sockfile.read(length)
print (garbage)
while True:
b = self.sockfile.read(1)
if b[0] == 0xff:
break
ret += b
return ret.decode('utf-8') #str(ret,'utf-8').encode('utf-8')
def close(self):
self.socket.close()
self.sockfile.close()
del self.socket
del self.sockfile
class WebSocketReader(threading.Thread):
def __init__(self,websocket,messagehandler):
self.ws = websocket
self.msgh = messagehandler
self.running = True
threading.Thread.__init__(self)
def run(self):
while(self.running):
try:
self.msgh( self.ws.recv() )
except Exception as e:
print (e)
def close(self):
self.running = False
class Uri:
def __init__(self,scheme,host,port,path,query = ''):
self.scheme = scheme
self.hostname = host
self.port = port
self.path = path
self.query = query
if __name__ == "__main__":
# ws = WebSocket('ws://localhost:8000/wstest')
tosend = 'foobåar æø Ø'
print (tosend)
ws = WebSocket( Uri('ws','10.211.55.2',8000,'/wstest') )
def handler(m):
print (m)
wsr = WebSocketReader(ws,handler)
ws.connect()
wsr.start()
ws.send( tosend )
print ('sleeping...')
# print( ws.recv() )
time.sleep(2)
print ('ok, slept')
ws.close()
wsr.close()
print ('websocket and reader closed!')