Add context handlers to Connection and use system ca_cert file
This commit is contained in:
parent
aee8e5c118
commit
f1314c7ec1
3 changed files with 24 additions and 8 deletions
|
|
@ -1,9 +1,10 @@
|
||||||
import logging
|
import logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.DEBUG,
|
level=logging.DEBUG,
|
||||||
format="[{levelname}] in {threadName: <13} <{name}>: {message}",
|
format="[{levelname: <5}] in {threadName: <13} <{name}>: {message}",
|
||||||
style="{"
|
style="{"
|
||||||
)
|
)
|
||||||
|
|
||||||
from .basic_types import Message, SessionView
|
from .basic_types import Message, SessionView
|
||||||
from .callbacks import Callbacks
|
from .callbacks import Callbacks
|
||||||
|
from .connection import Connection
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import ssl
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import websocket
|
import websocket
|
||||||
|
|
@ -7,6 +8,7 @@ from websocket import WebSocketException as WSException
|
||||||
|
|
||||||
from .callbacks import Callbacks
|
from .callbacks import Callbacks
|
||||||
|
|
||||||
|
SSLOPT_CA_CERTS = {'ca_certs': ssl.get_default_verify_paths().cafile}
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Connection():
|
class Connection():
|
||||||
|
|
@ -33,15 +35,23 @@ class Connection():
|
||||||
|
|
||||||
self.room = room
|
self.room = room
|
||||||
|
|
||||||
self._url_format = url_format or ROOM_FORMAT
|
self._url_format = url_format or Connection.ROOM_FORMAT
|
||||||
|
|
||||||
self._stopping = False
|
self._stopping = False
|
||||||
|
|
||||||
self._ws = None
|
self._ws = None
|
||||||
self._thread = None
|
self._thread = None
|
||||||
self._send_id = 0
|
self._send_id = 0
|
||||||
self._callbacks = callbacks.Callbacks()
|
self._callbacks = Callbacks()
|
||||||
self._id_callbacks = callbacks.Callbacks()
|
self._id_callbacks = Callbacks()
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self._lock.acquire()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
self._lock.release()
|
||||||
|
|
||||||
def _connect(self, tries=-1, delay=10):
|
def _connect(self, tries=-1, delay=10):
|
||||||
"""
|
"""
|
||||||
|
|
@ -59,21 +69,22 @@ class Connection():
|
||||||
while tries != 0:
|
while tries != 0:
|
||||||
try:
|
try:
|
||||||
url = self._url_format.format(self.room)
|
url = self._url_format.format(self.room)
|
||||||
logger.log("Connecting to url: {!r}".format(url))
|
logger.info("Connecting to url: {!r}".format(url))
|
||||||
logger.debug("{} {} left".format(
|
logger.debug("{} {} left".format(
|
||||||
tries-1 if tries>0 else "infinite",
|
tries-1 if tries>0 else "infinite",
|
||||||
"tries" if tries!=1 else "try" # proper english :D
|
"tries" if (tries-1)!=1 else "try" # proper english :D
|
||||||
))
|
))
|
||||||
self._ws = websocket.create_connection(
|
self._ws = websocket.create_connection(
|
||||||
url,
|
url,
|
||||||
enable_multithread=True
|
enable_multithread=True,
|
||||||
|
sslopt=SSLOPT_CA_CERTS
|
||||||
)
|
)
|
||||||
|
|
||||||
except WSException:
|
except WSException:
|
||||||
if tries > 0:
|
if tries > 0:
|
||||||
tries -= 1
|
tries -= 1
|
||||||
if tries != 0:
|
if tries != 0:
|
||||||
logger.log("Connection failed. Retrying in {} seconds.".format(delay))
|
logger.info("Connection failed. Retrying in {} seconds.".format(delay))
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
4
yaboli/messagedb.py
Normal file
4
yaboli/messagedb.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
class MessageDB():
|
||||||
|
pass
|
||||||
Loading…
Add table
Add a link
Reference in a new issue