-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonWebCore.py
805 lines (643 loc) · 29.4 KB
/
PythonWebCore.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
#!/usr/bin/env python3
# no support for ssl/wss
# not scalable
# uses ineff. thread to handle
# no limit on file service/danger
# Other - SSL:
# openssl genrsa -des3 -out server.orig.key 2048
# openssl rsa -in server.orig.key -out server.key
# openssl req -new -key server.key -out server.csr
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
# openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
# Other - Links:
# https://github.com/enthought/Python-2.7.3/blob/master/Lib/SimpleHTTPServer.py
# https://github.com/enthought/Python-2.7.3/blob/master/Lib/SimpleHTTPServer.py
# https://blog.anvileight.com/posts/simple-python-http-server/
# https://gist.github.com/bradmontgomery/2219997
# https://github.com/enthought/Python-2.7.3/blob/master/Lib/BaseHTTPServer.py
# https://www.afternerd.com/blog/python-http-server/
# https://www.acmesystems.it/python_http
# https://github.com/pikhovkin/simple-websocket-server/blob/master/simple_websocket_server/__init__.py
import socket
import hashlib
import base64
import time
import logging
import sys
import ssl
import json
from threading import Thread
import string
import random
import os
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
__responses = {
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols',
'Switching to new protocol; obey Upgrade header'),
200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted',
'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),
300: ('Multiple Choices',
'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified',
'Document has not changed since given time'),
305: ('Use Proxy',
'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'),
400: ('Bad Request',
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment Required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this resource.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),
500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
'The server cannot process the request due to a high load'),
504: ('Gateway Timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
}
# every connection is handled with a thread, may not scale well, but very easy to deal with
class SocketHanlderThread(Thread):
def __init__(self, socket, address, server):
Thread.__init__(self)
self.socket = socket
self.address = address
self.path = None
self.server = server
self.daemon = True
def parse_ContentDisposition(self, header):
extract = {}
subname = None
subvalue = None
i = 0
j = 0
lx = len(header)
for c in header:
j = j + 1
if c == ':' and "identifier" not in extract:
extract["identifier"] = header[i:j-1]
i = j + 1
continue
if c == "=" and not subname:
subname = header[i:j-1]
i = j
continue
if c == ';' and subname and not subvalue:
subvalue = header[i:j-1]
extract[subname] = subvalue
subname = None
subvalue = None
i = j + 1
continue
if lx == j and subname and not subvalue:
subvalue = header[i:j]
extract[subname] = subvalue
subname = None
subvalue = None
i = j + 1
continue
if c == ';':
extract[header[i:j-1]] = header[i:j-1]
i = j + 1
continue
if lx == j:
extract[header[i:j-1]] = header[i:j-1]
continue
return extract
def run(self):
try:
self.requestlines = []
self.headers = {}
# just about enough 4KiB to understand the request
request = self.socket.recv(4 * 1024)
if len(request) < 5:
return
# primary request with headers extraction
lend = 0
lbegin = 0
while 1:
lbegin = lend
lend = request.index(b'\r\n', lend)
if lbegin == lend:
lend = lend + len(b'\r\n')
break
else:
header = (request[lbegin:lend]).decode('ascii')
# todo prper header extraction
if header.startswith('Content-Length:'):
self.headers["Content-Length"] = int(header.split()[1])
elif header.startswith('Range: bytes'):
range = header[len('Range: bytes='):]
self.headers["Range-Start"], self.headers["Range-End"] = range.split('-')
elif header.startswith('Content-Type:'):
for t in header.split():
if t.startswith('application/x-www-form-urlencoded'):
self.headers["Content-Type"] = 'application/x-www-form-urlencoded'
elif t.startswith('multipart/form-data'):
self.headers["Content-Type"] = 'multipart/form-data'
elif t.startswith('application/json'):
self.headers["Content-Type"] = 'application/json'
elif t.startswith('charset'): # todo exctract charset
self.headers["Content-Type-charset"] = 'utf-8'
elif t.startswith('boundary'):
self.headers["Content-Type-boundary"] = t.split('=')[1]
elif header.startswith('Sec-WebSocket-Key:'):
self.headers["Sec-WebSocket-Key"] = header.split()[1]
elif header.startswith('Authorization:'):
self.headers["Authorization"] = header.split()[2]
elif header.startswith('Cookie:'):
self.headers["Cookie"] = header[len("Cookie: "):]
self.requestlines.append(header)
lend = lend + len(b'\r\n')
self.sessionid = None
# additional parsing
if "Cookie" in self.headers:
q = [h.split('=') for h in self.headers['Cookie'].split()]
cookies = dict((x,y.rstrip(';')) for x,y in q)
if 'sessionid' in cookies:
self.sessionid = cookies['sessionid']
if "Content-Type" in self.headers:
if self.headers["Content-Type"] == 'application/x-www-form-urlencoded':
self.data = request[lend:]
# receive rest of expected data, once match
while len(self.data) < self.headers["Content-Length"] :
self.socket.settimeout(30)
self.data = self.data + self.socket.recv(4 * 1024)
self.socket.settimeout(None)
t = self.data.decode('utf-8')
self.data = dict(item.split("=") for item in t.split("&"))
if self.headers["Content-Type"] == 'application/json':
self.data = request[lend:]
# receive rest of expected data, once match
while len(self.data) < self.headers["Content-Length"] :
self.socket.settimeout(30)
self.data = self.data + self.socket.recv(4 * 1024)
self.socket.settimeout(None)
t = self.data.decode('utf-8')
self.data = json.loads(t)
# https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
# yea, not easy, boundry might change, making things more complicated
if self.headers["Content-Type"] == 'multipart/form-data':
self.data = []
self.content = request[lend:]
while len(self.content) < self.headers["Content-Length"]:
self.socket.settimeout(30)
self.content = self.content + self.socket.recv(4 * 1024)
self.socket.settimeout(None)
lend = 0
lbegin = 0
formdata = {}
while 1:
lbegin = lend
lend = self.content.index(b'\r\n', lend)
if lbegin == lend:
lend = lend + len(b'\r\n')
lbegin = lend
lend = self.content.index(b'\r\n' + bytes(formdata["Content-Disposition-boundary"], "ascii"), lend)
formdata["Content"] = self.content[lbegin:lend]
lend = lend + len(b'\r\n')
self.data.append(formdata)
formdata = {}
else:
header = (self.content[lbegin:lend]).decode('ascii')
if header.endswith(self.headers["Content-Type-boundary"] + '--'):
break
elif header.startswith('--' + self.headers["Content-Type-boundary"]):
formdata["Content-Disposition-boundary"] = '--' + self.headers["Content-Type-boundary"]
elif header.startswith('Content-Disposition:'):
t = self.parse_ContentDisposition(header)
if 'form-data' in t:
formdata["Content-Disposition"] = 'form-data'
if 'name' in t:
formdata["Content-Disposition-name"] = t['name'].strip('"')
if 'filename' in t:
formdata["Content-Disposition-filename"] = t['filename'].strip('"')
elif header.startswith('Content-Type:'):
formdata["Content-Type"] = header.split()[1]
lend = lend + len(b'\r\n')
self.command, self.route, self.version = None, None, None
# extracting verb, route, version
if len(self.requestlines) > 0:
logging.info(self.requestlines[0])
words = self.requestlines[0].split()
if len(words) == 3:
self.command, self.route, self.version = words
elif len(words) == 2:
self.command, self.route = words
self.server.endpoint.Respond(self)
except BrokenPipeError as ex:
logging.error(ex)
except Exception as ex:
logging.critical(ex)
if self.server.exceptionEndpoint:
self.server.ExceptionEndpoint.Respond(self)
finally:
self.socket.close()
class Endpoint(object):
def __init__(self):
self.Owner = None
def randomString(self, stringLength=64):
"""Generate a random string of fixed length """
letters = string.ascii_lowercase + string.digits
return ''.join(random.choice(letters) for i in range(stringLength))
def CanRespond(self, sockth):
return True
def Respond(self, sockth):
return True
class RouteEndpoint(Endpoint):
def __init__(self, route):
super().__init__()
self.route = route
def CanRespond(self, sockth):
t = super().CanRespond(sockth)
t &= sockth.route == self.route
return t
def Respond(self, sockth):
return super().Respond(sockth)
class MultiRouteEndpoint(Endpoint):
def __init__(self, endpoints):
super().__init__()
self.endpoints = endpoints
for e in self.endpoints:
e.Owner = self
def Respond(self, sockth):
t = super().Respond(sockth)
for e in self.endpoints:
if e.CanRespond(sockth):
e.Respond(sockth)
break
return True
class WWWAuthenticateBasicEndpoint(Endpoint):
def __init__(self, username, password , passEndpoint):
super().__init__()
self.username = username
self.password = password
self.sessions = {}
self.passEndpoint = passEndpoint
self.passEndpoint.Owner = self
def Authenticate(self, sockth):
if self.sessions[sockth.sessionid] == False:
if 'Authorization' in sockth.headers:
token = base64.b64decode(sockth.headers['Authorization']).decode("ascii")
if token == self.username + ':' + self.password:
self.sessions[sockth.sessionid] = True
return self.sessions[sockth.sessionid]
def Respond(self, sockth):
t = super().Respond(sockth)
if sockth.sessionid == None or sockth.sessionid != None and sockth.sessionid not in self.sessions:
sockth.sessionid = self.randomString()
logging.debug('new session ' + sockth.sessionid)
self.sessions[sockth.sessionid] = False
sockth.socket.send(b'HTTP/1.1 401 Unauthorized\r\n')
sockth.socket.send(b'WWW-Authenticate: Basic realm="User Visible Realm", charset="UTF-8"\r\n')
sockth.socket.send(b'Set-Cookie: sessionid=' + bytes(str(sockth.sessionid), "ascii") + b'\r\n')
sockth.socket.send(b'Connection: Closed\r\n')
sockth.socket.send(b'\r\n')
sockth.socket.send(b'')
return False
if self.Authenticate(sockth):
self.passEndpoint.Respond(sockth)
else:
# if self.failEndpoint:
# self.failEndpoint.Respond(sockth)
# self.sessions.pop(sockth.sessionid)
# sockth.socket.send(b'Set-Cookie: sessionid=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT\r\n')
# else:
sockth.socket.send(b'HTTP/1.1 401 Unauthorized\r\n')
sockth.socket.send(b'WWW-Authenticate: Basic realm="User Visible Realm", charset="UTF-8"\r\n')
sockth.socket.send(b'Connection: Closed\r\n')
sockth.socket.send(b'\r\n')
sockth.socket.send(b'Login failed!')
return True
class StaticEndpoint(Endpoint):
def __init__(self, content):
super().__init__()
self.content = content
def Respond(self, sockth):
super().Respond(sockth)
sockth.socket.send(b'HTTP/1.1 200 OK\r\n')
sockth.socket.send(b'Content-Length: ' + bytes(str(len(self.content)), "ascii") + b'\r\n')
sockth.socket.send(b'Connection: Closed\r\n')
sockth.socket.send(b'\r\n')
sockth.socket.send(bytes(self.content, "ascii"))
class StaticFileEndpoint(Endpoint):
def __init__(self, path, cache = False):
super().__init__()
self.path = path
def CanRespond(self, sockth):
t = super().CanRespond(sockth)
t &= os.path.exists(self.path + sockth.route)
return t
def Respond(self, sockth):
lenFile = 0
partial = False
start = 0
end = 0
lenBuff = 16*1024*1024
if 'Range-Start' in sockth.headers:
start = int(sockth.headers['Range-Start'])
if 'Range-End' in sockth.headers:
if sockth.headers['Range-End'] != '':
end = int(sockth.headers['Range-End'])
lenBuff = end - start + 1
try:
lenFile = os.path.getsize(self.path + sockth.route)
# todo implement larger file streaming
if lenFile > lenBuff:
partial = True
with open(self.path + sockth.route, 'rb') as f:
f.seek(start)
content = f.read(lenBuff)
lenBuff = len(content)
if partial and end == 0:
end = start + lenBuff - 1
except FileNotFoundError as ex:
logging.critical(ex)
sockth.exception = ex
sockth.socket.send(b'HTTP/1.1 404 Not Found\r\n')
return False
mime = 'application/octet-stream'
# use import mimetypes if gets complicated
if sockth.route.endswith(".html"):
mime = 'text/html'
elif sockth.route.endswith(".ico"):
mime = 'image/x-icon'
elif sockth.route.endswith(".css"):
mime = 'text/css'
elif sockth.route.endswith(".jpg"):
mime = 'image/jpeg'
elif sockth.route.endswith(".js"):
mime = 'application/javascript'
elif sockth.route.endswith(".mp4"):
mime = 'video/mp4'
if partial:
sockth.socket.send(b'HTTP/1.1 206 Partial Content\r\n')
else:
sockth.socket.send(b'HTTP/1.1 200 OK\r\n')
if partial:
sockth.socket.send(b'Accept-Ranges: bytes\r\n')
sockth.socket.send(b'Content-Length: ' + bytes(str(lenBuff), "ascii") + b'\r\n')
sockth.socket.send(b'Content-Type: ' + bytes(mime, "ascii") + b'\r\n')
if partial:
sockth.socket.send(b'Content-Range: bytes ' + bytes(str(start), "ascii") + b'-' + bytes(str(end), "ascii") + b'/' + bytes(str(lenFile), "ascii") + b'\r\n')
sockth.socket.send(b'Connection: Closed\r\n')
sockth.socket.send(b'\r\n')
sockth.socket.send(content)
return True
class WebServiceEndpoint(RouteEndpoint):
def __init__(self, route, callback):
super().__init__(route)
self.callback = callback
def OnReady(self, sockth):
# urlparse.parse_qs("Name1=Value1;Name2=Value2;Name3=Value3")
method = self.callback
content = method(**sockth.data)
lenContent = bytes(str(len(content)), "ascii")
sockth.socket.send(b'HTTP/1.1 200 OK\r\n')
sockth.socket.send(b'Content-Length: ' + lenContent + b'\r\n')
sockth.socket.send(b'Connection: Closed\r\n')
sockth.socket.send(b'\r\n')
if content:
sockth.socket.send(content)
def Respond(self, sockth):
if not super().Respond(sockth):
return False
self.OnReady(sockth)
class ServerSentEventEndpoint(RouteEndpoint):
# https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
# * IE, Edge Not supported
# event: A string identifying the type of event described. If this is specified, an event will be dispatched on the browser to the listener for the specified event name;
# the website source code should use addEventListener() to listen for named events. The onmessage handler is called if no event name is specified for a message.
# data: The data field for the message. When the EventSource receives multiple consecutive lines that begin with data:,
# it will concatenate them, inserting a newline character between each one. Trailing newlines are removed.
# id: The event ID to set the EventSource object's last event ID value.
# retry: The reconnection time to use when attempting to send the event. This must be an integer, specifying the reconnection time in milliseconds.
# If a non-integer value is specified, the field is ignored.
def __init__(self, route):
super().__init__(route)
def Send(self, sockth, id, event, data, retry):
if id:
sockth.socket.send(b'id: ' + id + b'\r\n')
if event:
sockth.socket.send(b'event: ' + event + b'\r\n')
if data:
sockth.socket.send(b'data: ' + data + b'\r\n')
if retry:
sockth.socket.send(b'retry: ' + retry + b'\r\n')
sockth.socket.send(b'\r\n')
def OnReady(self, sockth):
while 1:
self.Send(sockth, None, None, b'Server Side Event - default message', None)
time.sleep(1)
self.Send(sockth, None, b'customevent', b'Server Side Event - customevent', None)
time.sleep(1)
def Respond(self, sockth):
if not super().Respond(sockth):
return False
sockth.socket.send(b'HTTP/1.1 200 OK\r\n')
sockth.socket.send(b'Content-Type: text/event-stream\r\n')
sockth.socket.send(b'Cache-Control: no-cache\r\n')
sockth.socket.send(b'\r\n')
self.OnReady(sockth)
class WebSocketEndpoint(RouteEndpoint):
def __init__(self, route, onReady):
super().__init__(route)
self.onReady = onReady
def Receive(self, sockth):
# todo implement big packets recev
# partial recv looks like wont happen, how ever multiple packets may need to be received and combined
request = sockth.socket.recv(4096)
fin = request[0] & 0x80 == 128
opcode = request[0] & (0xF)
Mask = request[1] & 0x80 == 128
payload = bytearray()
plMask = None
plFlag = request[1] & 0x7F
plLen = 0
plStart = 0
if plFlag < 126:
plLen = plFlag
plStart = 2
if Mask:
plMask = [request[2], request[3], request[4], request[5]]
plStart = 2 + 4
elif plFlag == 126:
plLen = (request[2] << 8) + request[3]
plStart = 4
if Mask:
plMask = [request[4], request[5], request[6], request[7]]
plStart = 4 + 4
elif plFlag == 127:
plLen = (request[2] << 24) + (request[3] <<
16) + (request[4] << 8) + request[5]
plStart = 6
if Mask:
plMask = [request[6], request[7], request[8], request[9]]
plStart = 6 + 4
i = 0
for b in request[plStart:]:
if Mask:
payload.append(b ^ plMask[i % 4])
else:
payload.append(b)
i = i + 1
plLen = plLen - 1
return payload
def Send(self, sockth, payload):
pllen = len(payload)
plbytes = bytearray()
b1, b2 = 0, 0
opcode = 0x1
fin = 1
b1 = opcode | fin << 7
if pllen < 125:
b2 |= pllen
elif pllen > 124:
raise NotImplementedError
plbytes.append(b1)
plbytes.append(b2)
plbytes.extend(payload)
sockth.socket.send(plbytes)
def OnReady(self, sockth):
while 1:
response = self.Receive(sockth)
if response:
self.Send(sockth, response)
def Respond(self, sockth):
if not super().Respond(sockth):
return False
if "Sec-WebSocket-Key" in sockth.headers:
skey = sockth.headers["Sec-WebSocket-Key"]
stoken = skey + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' # MAGICSTRING
stokensha1 = hashlib.sha1(stoken.encode('utf-8'))
secWebSocketAccept = base64.b64encode(stokensha1.digest())
sockth.socket.send(b'HTTP/1.1 101 Switching Protocols\r\n')
sockth.socket.send(b'Upgrade: websocket\r\n')
sockth.socket.send(b'Connection: Upgrade\r\n')
sockth.socket.send(b'Sec-WebSocket-Accept: ' + secWebSocketAccept + b'\r\n')
sockth.socket.send(b'\r\n')
self.OnReady(sockth)
else:
sockth.exception = Exception("No Sec-WebSocket-Key in headers")
class PythonWebCore(Thread):
def __init__(self, hostname, port = 80, cert = None):
Thread.__init__(self)
self.endpoint = None
self.hostname = hostname
self.port = port
self.daemon = True
self.listening = False
self.cert = cert
self.exceptionEndpoint = None
def RegisterEndpoint(self, endpoint):
logging.debug('registering endpoint ' + type(endpoint).__name__)
self.endpoint = endpoint
return endpoint
def stop(self):
"""
properly kills the process: https://stackoverflow.com/a/16736227/4225229
"""
self.listening = False
time.sleep(1)
# connect again to release the listener for terminating the connection
t = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
t.connect((self.hostname, self.port))
t.close()
def run(self):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socksrvr:
socksrvr.bind((self.hostname, self.port))
socksrvr.listen(5) # max backlog of connections
self.listening = True
logging.info('listening on ' + self.hostname + ':' + str(self.port))
while self.listening:
try:
sockclient, addr = socksrvr.accept()
if self.cert:
try:
sockclient = ssl.wrap_socket(sockclient, certfile = self.cert, server_side = True)
except Exception as ex:
logging.critical(ex)
# client rejects the certifcate?
if ex.args[1].startswith('[SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN]'):
continue
# a regular socket was connect to secure endpoint
if ex.args[1].startswith('[SSL: HTTP_REQUEST]'):
continue
logging.debug('connect ' + ':'.join(str(x) for x in addr))
SocketHanlderThread(sockclient, addr, self).start()
except Exception as ex:
logging.critical(ex)
socksrvr.close()
break
if __name__ == '__main__':
def TestSocket(payload):
p = payload.decode("ascii")
logging.debug(p)
return bytes(p, "utf-8")
def TestAjax(param1, param2, param3):
logging.debug('%s %s %s ', param1, param2, param3)
return b'OK'
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - [%(levelname)-5.5s] - %(message)s')
pwc = PythonWebCore('', 65125)
# pwc.RegisterEndpoint()
#
# # ws.OnReady = TestCustomOnReady
from os import path as ospath
ep_staticfile = StaticFileEndpoint(ospath.dirname(ospath.abspath(__file__)) + '/contents')
ep_content = StaticEndpoint('Login Failed!')
ep_webservice = WebServiceEndpoint('/TestAjax', TestAjax)
ep_serverevent = ServerSentEventEndpoint('/TestSSE')
ep_websocket = WebSocketEndpoint('/TestSocket', TestSocket)
ep_multiroute = MultiRouteEndpoint([ep_webservice, ep_serverevent, ep_websocket, ep_staticfile])
ep_authenticate = WWWAuthenticateBasicEndpoint("admin", "admin", ep_multiroute)
pwc.RegisterEndpoint(ep_authenticate)
pwc.start()
logging.debug('http://localhost:' + str(pwc.port) + '/PythonWebCore.html' + ' login using admin/admin')
logging.info('Press Ctrl+C to terminate.')
try:
pwc.join()
except KeyboardInterrupt as ex:
logging.critical(ex)
pwc.stop()
pwc.join()
except Exception as ex:
logging.critical(ex)
pwc.stop()
pwc.join()