Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions blynklib_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__version__ = '0.2.6'

import usocket as socket
import ussl as ssl
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ussl import can affect library usage on
some micropython ports ..
I suggest create separate file
blynklib_mp_ssl.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about another split. I mean I think even the first split was a bad idea because the common code never gets in sync anymore.

What about I import it inline in _get_socket() just before using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or.. I can have the new blynklib_mp_ssl.py which has a new class SSLConnection which inherit from class Connection.

The compromise is that people using the SSL version will need to upload 2 files instead of 1. Is that bad?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed new commits showing the concept with the new SslConnection class.

Blynk can still be used as usual for clear text transmissions. with no additional imports or anything.

For SSL, it is used like so:

import blynklib_mp as blynklib
import blynklib_mp_ssl as blynklib_ssl

ssl_connection = blynklib_ssl.SslConnection(token, port=443, log=print)
blynk = blynklib.Blynk(secret.BLYNK_AUTH, connection=ssl_connection)

We also don't need to switch on the port used.

What do you think?

import utime as time
import ustruct as struct
import uselect as select
Expand Down Expand Up @@ -56,7 +57,7 @@ class Protocol(object):
STATUS_OK = const(200)
VPIN_MAX_NUM = const(32)

_msg_id = 1
_msg_id = 0

def _get_msg_id(self, **kwargs):
if 'msg_id' in kwargs:
Expand Down Expand Up @@ -125,6 +126,7 @@ def internal_msg(self, *args):
class Connection(Protocol):
SOCK_MAX_TIMEOUT = const(5)
SOCK_TIMEOUT = 0.05
SOCK_SSL_TIMEOUT = const(1)
EAGAIN = const(11)
ETIMEDOUT = const(60)
RETRIES_TX_DELAY = const(2)
Expand Down Expand Up @@ -164,15 +166,28 @@ def send(self, data):
try:
retries -= 1
self._last_send_time = ticks_ms()
return self._socket.send(data)
try:
bytes_written = self._socket.send(data)
except AttributeError:
bytes_written = self._socket.write(data)
return bytes_written
except (IOError, OSError):
sleep_ms(self.RETRIES_TX_DELAY)

def receive(self, length, timeout):
d_buff = b''
try:
self._set_socket_timeout(timeout)
d_buff += self._socket.recv(length)
try:
d_buff += self._socket.recv(length)
except AttributeError:
timeout = self.SOCK_SSL_TIMEOUT
while not d_buff and timeout > 0:
ret = self._socket.read(length)
if ret:
d_buff += ret
timeout -= self.SOCK_TIMEOUT
time.sleep(self.SOCK_TIMEOUT)
if len(d_buff) >= length:
d_buff = d_buff[:length]
return d_buff
Expand Down Expand Up @@ -203,6 +218,13 @@ def _get_socket(self):
self._socket = socket.socket()
self._socket.connect(socket.getaddrinfo(self.server, self.port)[0][-1])
self._set_socket_timeout(self.SOCK_TIMEOUT)
if self.port == 443 or self.port == 8443:
self.log('Using SSL socket...')
self._socket = ssl.wrap_socket(self._socket)
# Short reads are not supported in ssl mode. We work around
# this by setting the socket non blocking and doing manual
# polling/timeout.
self._socket.setblocking(False)
self.log('Connected to server')
except Exception as g_exc:
raise BlynkError('Server connection failed: {}'.format(g_exc))
Expand Down