Skip to content

Commit

Permalink
Fix SSL certificate error
Browse files Browse the repository at this point in the history
  • Loading branch information
Musicted committed Dec 8, 2021
1 parent d380d65 commit 28b3a78
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ A Python Lighthouse Adapter

(README WIP; please consult pyghthouse/ph.py for usage information.)

Example scripts can be found [here](examples)
Example scripts can be found [here](examples)

## Troubeshooting

If you are experiencing errors regarding SSL certificates,
instantiate Pyghthouse using the `ignore_ssl_cert=True`
keyword argument.
7 changes: 5 additions & 2 deletions pyghthouse/connection/wsconnector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from threading import Thread, Lock
from websocket import WebSocketApp, setdefaulttimeout, ABNF
from msgpack import packb, unpackb
from ssl import CERT_NONE


class WSConnector:
Expand All @@ -17,7 +18,7 @@ def __next__(self):
def __iter__(self):
return self

def __init__(self, username: str, token: str, address: str, on_msg=None):
def __init__(self, username: str, token: str, address: str, on_msg=None, ignore_ssl_cert=False):
self.username = username
self.token = token
self.address = address
Expand All @@ -26,6 +27,7 @@ def __init__(self, username: str, token: str, address: str, on_msg=None):
self.lock = Lock()
self.reid = self.REID()
self.running = False
self.ignore_ssl_cert = ignore_ssl_cert
setdefaulttimeout(60)

def send(self, data):
Expand All @@ -38,7 +40,8 @@ def start(self):
on_message=None if self.on_msg is None else self._handle_msg,
on_open=self._ready, on_error=self._fail)
self.lock.acquire()
Thread(target=self.ws.run_forever).start()
kwargs = {"sslopt": {"cert_reqs": CERT_NONE}} if self.ignore_ssl_cert else None
Thread(target=self.ws.run_forever, kwargs=kwargs).start()

def _fail(self, ws, err):
self.lock.release()
Expand Down
6 changes: 4 additions & 2 deletions pyghthouse/ph.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ def run(self):
self.parent.connector.send(self.parent.canvas.get_image_bytes())

def __init__(self, username: str, token: str, address: str = "wss://lighthouse.uni-kiel.de/websocket",
frame_rate: float = 30.0, image_callback=None, verbosity=VerbosityLevel.WARN_ONCE):
frame_rate: float = 30.0, image_callback=None, verbosity=VerbosityLevel.WARN_ONCE,
ignore_ssl_cert=False):
if frame_rate > 60.0 or frame_rate <= 0:
raise ValueError("Frame rate must be greater than 0 and at most 60.")
self.username = username
Expand All @@ -196,7 +197,8 @@ def __init__(self, username: str, token: str, address: str = "wss://lighthouse.u
self.image_callback = image_callback
self.canvas = PyghthouseCanvas()
self.msg_handler = self.PHMessageHandler(verbosity)
self.connector = WSConnector(username, token, address, on_msg=self.msg_handler.handle)
self.connector = WSConnector(username, token, address, on_msg=self.msg_handler.handle,
ignore_ssl_cert=ignore_ssl_cert)
self.config_lock = Lock()
self.ph_thread = None
signal(SIGINT, self._handle_sigint)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='pyghthouse',
version='0.2.0',
version='0.2.1',
packages=find_packages(where='.'),
url='https://github.com/Musicted/pyghthouse',
license='MIT',
Expand Down

0 comments on commit 28b3a78

Please sign in to comment.