Skip to content

Instantly share code, notes, and snippets.

@SevenW
Last active October 23, 2023 13:10
Show Gist options
  • Save SevenW/47be2f9ab74cac26bf21 to your computer and use it in GitHub Desktop.
Save SevenW/47be2f9ab74cac26bf21 to your computer and use it in GitHub Desktop.
HTTPWebSocketsHandler extends SimpleHTTPServer with WebSockets enabling the use of HTTP and WebSockets throught the same port
'''
The MIT License (MIT)
Copyright (C) 2014, 2015 Seven Watt <info@sevenwatt.com>
<http://www.sevenwatt.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
from SimpleHTTPServer import SimpleHTTPRequestHandler
import struct
from base64 import b64encode
from hashlib import sha1
from mimetools import Message
from StringIO import StringIO
import errno, socket #for socket exceptions
import threading
class WebSocketError(Exception):
pass
class HTTPWebSocketsHandler(SimpleHTTPRequestHandler):
_ws_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
_opcode_continu = 0x0
_opcode_text = 0x1
_opcode_binary = 0x2
_opcode_close = 0x8
_opcode_ping = 0x9
_opcode_pong = 0xa
mutex = threading.Lock()
def on_ws_message(self, message):
"""Override this handler to process incoming websocket messages."""
pass
def on_ws_connected(self):
"""Override this handler."""
pass
def on_ws_closed(self):
"""Override this handler."""
pass
def send_message(self, message):
self._send_message(self._opcode_text, message)
def setup(self):
SimpleHTTPRequestHandler.setup(self)
self.connected = False
# def finish(self):
# #needed when wfile is used, or when self.close_connection is not used
# #
# #catch errors in SimpleHTTPRequestHandler.finish() after socket disappeared
# #due to loss of network connection
# try:
# SimpleHTTPRequestHandler.finish(self)
# except (socket.error, TypeError) as err:
# self.log_message("finish(): Exception: in SimpleHTTPRequestHandler.finish(): %s" % str(err.args))
# def handle(self):
# #needed when wfile is used, or when self.close_connection is not used
# #
# #catch errors in SimpleHTTPRequestHandler.handle() after socket disappeared
# #due to loss of network connection
# try:
# SimpleHTTPRequestHandler.handle(self)
# except (socket.error, TypeError) as err:
# self.log_message("handle(): Exception: in SimpleHTTPRequestHandler.handle(): %s" % str(err.args))
def checkAuthentication(self):
auth = self.headers.get('Authorization')
if auth != "Basic %s" % self.server.auth:
self.send_response(401)
self.send_header("WWW-Authenticate", 'Basic realm="Plugwise"')
self.end_headers();
return False
return True
def do_GET(self):
if self.server.auth and not self.checkAuthentication():
return
if self.headers.get("Upgrade", None) == "websocket":
self._handshake()
#This handler is in websocket mode now.
#do_GET only returns after client close or socket error.
self._read_messages()
else:
SimpleHTTPRequestHandler.do_GET(self)
def _read_messages(self):
while self.connected == True:
try:
self._read_next_message()
except (socket.error, WebSocketError), e:
#websocket content error, time-out or disconnect.
self.log_message("RCV: Close connection: Socket Error %s" % str(e.args))
self._ws_close()
except Exception as err:
#unexpected error in websocket connection.
self.log_error("RCV: Exception: in _read_messages: %s" % str(err.args))
self._ws_close()
def _read_next_message(self):
#self.rfile.read(n) is blocking.
#it returns however immediately when the socket is closed.
try:
self.opcode = ord(self.rfile.read(1)) & 0x0F
length = ord(self.rfile.read(1)) & 0x7F
if length == 126:
length = struct.unpack(">H", self.rfile.read(2))[0]
elif length == 127:
length = struct.unpack(">Q", self.rfile.read(8))[0]
masks = [ord(byte) for byte in self.rfile.read(4)]
decoded = ""
for char in self.rfile.read(length):
decoded += chr(ord(char) ^ masks[len(decoded) % 4])
self._on_message(decoded)
except (struct.error, TypeError) as e:
#catch exceptions from ord() and struct.unpack()
if self.connected:
raise WebSocketError("Websocket read aborted while listening")
else:
#the socket was closed while waiting for input
self.log_error("RCV: _read_next_message aborted after closed connection")
pass
def _send_message(self, opcode, message):
try:
#use of self.wfile.write gives socket exception after socket is closed. Avoid.
self.request.send(chr(0x80 + opcode))
length = len(message)
if length <= 125:
self.request.send(chr(length))
elif length >= 126 and length <= 65535:
self.request.send(chr(126))
self.request.send(struct.pack(">H", length))
else:
self.request.send(chr(127))
self.request.send(struct.pack(">Q", length))
if length > 0:
self.request.send(message)
except socket.error, e:
#websocket content error, time-out or disconnect.
self.log_message("SND: Close connection: Socket Error %s" % str(e.args))
self._ws_close()
except Exception as err:
#unexpected error in websocket connection.
self.log_error("SND: Exception: in _send_message: %s" % str(err.args))
self._ws_close()
def _handshake(self):
headers=self.headers
if headers.get("Upgrade", None) != "websocket":
return
key = headers['Sec-WebSocket-Key']
digest = b64encode(sha1(key + self._ws_GUID).hexdigest().decode('hex'))
self.send_response(101, 'Switching Protocols')
self.send_header('Upgrade', 'websocket')
self.send_header('Connection', 'Upgrade')
self.send_header('Sec-WebSocket-Accept', str(digest))
self.end_headers()
self.connected = True
#self.close_connection = 0
self.on_ws_connected()
def _ws_close(self):
#avoid closing a single socket two time for send and receive.
self.mutex.acquire()
try:
if self.connected:
self.connected = False
#Terminate BaseHTTPRequestHandler.handle() loop:
self.close_connection = 1
#send close and ignore exceptions. An error may already have occurred.
try:
self._send_close()
except:
pass
self.on_ws_closed()
else:
self.log_message("_ws_close websocket in closed state. Ignore.")
pass
finally:
self.mutex.release()
def _on_message(self, message):
#self.log_message("_on_message: opcode: %02X msg: %s" % (self.opcode, message))
# close
if self.opcode == self._opcode_close:
self.connected = False
#Terminate BaseHTTPRequestHandler.handle() loop:
self.close_connection = 1
try:
self._send_close()
except:
pass
self.on_ws_closed()
# ping
elif self.opcode == self._opcode_ping:
_send_message(self._opcode_pong, message)
# pong
elif self.opcode == self._opcode_pong:
pass
# data
elif (self.opcode == self._opcode_continu or
self.opcode == self._opcode_text or
self.opcode == self._opcode_binary):
self.on_ws_message(message)
def _send_close(self):
#Dedicated _send_close allows for catch all exception handling
msg = bytearray()
msg.append(0x80 + self._opcode_close)
msg.append(0x00)
self.request.send(msg)
#main program and imports for standalone purpose
import sys
import threading
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import ssl
from base64 import b64encode
from HTTPWebSocketsHandler import HTTPWebSocketsHandler
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = 8000
if len(sys.argv) > 2:
secure = str(sys.argv[2]).lower()=="secure"
else:
secure = False
if len(sys.argv) > 3:
credentials = str(sys.argv[3])
else:
credentials = ""
class WSSimpleEcho(HTTPWebSocketsHandler):
def on_ws_message(self, message):
if message is None:
message = ''
# echo message back to client
self.send_message(str(message))
self.log_message('websocket received "%s"',str(message))
def on_ws_connected(self):
self.log_message('%s','websocket connected')
def on_ws_closed(self):
self.log_message('%s','websocket closed')
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
def _ws_main():
try:
#Replace WSSimpleEcho with your own subclass of HTTPWebSocketHandler
server = ThreadedHTTPServer(('', port), WSSimpleEcho)
server.daemon_threads = True
server.auth = b64encode(credentials)
if secure:
server.auth = b64encode(credentials)
server.socket = ssl.wrap_socket (server.socket, certfile='./server.pem', server_side=True)
print('started secure https server at port %d' % (port,))
else:
print('started http server at port %d' % (port,))
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down server')
server.socket.close()
if __name__ == '__main__':
_ws_main()
@SevenW
Copy link
Author

SevenW commented Sep 18, 2014

A wanted to make a basic HTTP server in python, being able to serve WebSockets, on the same port as the webserver is running on. Also I preferred not to use external libraries for ease of deployment. Several examples on the web (two mentioned below) could only deal with WebSockets at a given port. I made several attempts to extend these examples to serving webpages as well. After a while I started to understand servers and handlers, and decided to build a websocket implementation derived from SimpleHTTPRequestHandler, which turned out to work reliably.

Use (with optional port)

python HTTPWebSocketHandler.py 8001

main() is configured to echo the WebSocket messages.

The code is derived from both:
Jamie Kirkpatrick's jkp/websocketsserver.py https://gist.github.com/jkp/3136208
and
Opiate's SimpleWebSocketServer https://github.com/opiate/SimpleWebSocketServer

@pacmac
Copy link

pacmac commented Oct 14, 2014

Hi, I have been testing your class and found that if a client disconnects it throws an exception:

Exception happened during processing of request from ('127.0.0.1', 57053)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 595, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.handle()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 342, in handle
    self.handle_one_request()
  File "/usr/share/scripts/py/HTTPWebSocketsHandler.py", line 46, in handle_one_request
    self.read_next_message()
  File "/usr/share/scripts/py/HTTPWebSocketsHandler.py", line 61, in read_next_message
    self.opcode = ord(self.rfile.read(1)) & 0x0F
TypeError: ord() expected a character, but string of length 0 found
----------------------------------------

Any chance this can be handled gracefully ?

@SevenW
Copy link
Author

SevenW commented May 25, 2015

The issue mentioned by pacman above is solved.

Updated with:

  • Exception handling to deal with broken connections, for example due to standby mode of clients.
  • Support for SSL/HTTPS
  • Code clean-up

For HTTPS, a certificate needs to be installed in the same folder as the python scripts, which serves a the webroot in this example.

I have added the same code in a github repository for future maintenance:
https://github.com/SevenW/httpwebsockethandler

A more elaborate use of this basic Web and WebSocket server can be found in my Plugwise code:
https://github.com/SevenW/Plugwise-2-py

SevenW

@SevenW
Copy link
Author

SevenW commented May 26, 2015

command line options

nohup python HTTPWebSocketHandler.py 8001 secure user:password >>ws.log&

#no username and password requested
nohup python HTTPWebSocketHandler.py 8000 secure >>ws.log&

#plain http, with optional port
nohup python HTTPWebSocketHandler.py 8002 >>ws.log&

#plain http, default port 8000
nohup python HTTPWebSocketHandler.py >>ws.log&

@andyhasit
Copy link

Hi, here's a fix for line 86 as it gives an AttributeError:

if 'auth' in self.server.__dict__ and not self.checkAuthentication():
        return

Also, line 234 doesn't need to be there if all in one file:

from HTTPWebSocketsHandler import HTTPWebSocketsHandler

@gennady-melesh
Copy link

It doesn't work on Firefox
Should set to HTTP protocol version
self.protocol_version = 'HTTP/1.1'
https://datatracker.ietf.org/doc/html/rfc7231#section-6.2.2
By default SimpleHTTPServer responces HTTP 1.0, which isn't include 101 code by specification
https://datatracker.ietf.org/doc/html/rfc1945#section-9.1

@Beuchey
Copy link

Beuchey commented Feb 15, 2022

Have anyone being able to make this work? I keep getting an exception because the rfile is empty on every handshake attempt from a simple websocket client in javascript included in a HTML page served by this server.

@parkhw00
Copy link

Following modification required for ubuntu 22.04, python 3.10.

-from SimpleHTTPServer import SimpleHTTPRequestHandler
+from http.server import SimpleHTTPRequestHandler
 import struct
 from base64 import b64encode
 from hashlib import sha1
-from mimetools import Message
-from StringIO import StringIO
 import errno, socket #for socket exceptions
 import threading

@@ -73,18 +71,7 @@
         # except (socket.error, TypeError) as err:
             # self.log_message("handle(): Exception: in SimpleHTTPRequestHandler.handle(): %s" % str(err.args))

-    def checkAuthentication(self):
-        auth = self.headers.get('Authorization')
-        if auth != "Basic %s" % self.server.auth:
-            self.send_response(401)
-            self.send_header("WWW-Authenticate", 'Basic realm="Plugwise"')
-            self.end_headers();
-            return False
-        return True
-
     def do_GET(self):
-        if self.server.auth and not self.checkAuthentication():
-            return
         if self.headers.get("Upgrade", None) == "websocket":
             self._handshake()
             #This handler is in websocket mode now.
@@ -97,7 +84,7 @@
         while self.connected == True:
             try:
                 self._read_next_message()
-            except (socket.error, WebSocketError), e:
+            except (socket.error, WebSocketError) as e:
                 #websocket content error, time-out or disconnect.
                 self.log_message("RCV: Close connection: Socket Error %s" % str(e.args))
                 self._ws_close()
@@ -116,10 +103,10 @@
                 length = struct.unpack(">H", self.rfile.read(2))[0]
             elif length == 127:
                 length = struct.unpack(">Q", self.rfile.read(8))[0]
-            masks = [ord(byte) for byte in self.rfile.read(4)]
+            masks = [byte for byte in self.rfile.read(4)]
             decoded = ""
             for char in self.rfile.read(length):
-                decoded += chr(ord(char) ^ masks[len(decoded) % 4])
+                decoded += chr(char ^ masks[len(decoded) % 4])
             self._on_message(decoded)
         except (struct.error, TypeError) as e:
             #catch exceptions from ord() and struct.unpack()
@@ -145,7 +132,7 @@
                 self.request.send(struct.pack(">Q", length))
             if length > 0:
                 self.request.send(message)
-        except socket.error, e:
+        except socket.error as e:
             #websocket content error, time-out or disconnect.
             self.log_message("SND: Close connection: Socket Error %s" % str(e.args))
             self._ws_close()
@@ -159,7 +146,10 @@
         if headers.get("Upgrade", None) != "websocket":
             return
         key = headers['Sec-WebSocket-Key']
-        digest = b64encode(sha1(key + self._ws_GUID).hexdigest().decode('hex'))
+        key += self._ws_GUID
+        h = sha1()
+        h.update(key.encode())
+        digest = b64encode(h.digest()).decode()
         self.send_response(101, 'Switching Protocols')
         self.send_header('Upgrade', 'websocket')
         self.send_header('Connection', 'Upgrade')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment