Skip to content

Commit b24de1f

Browse files
author
Ronald Dehuysser
committed
Fix SSL errors
1 parent 816155f commit b24de1f

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

app/httpclient.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import usocket, os
1+
import usocket, os, time
22

33
class Response:
44

@@ -7,13 +7,19 @@ def __init__(self, socket, saveToFile=None):
77
self._saveToFile = saveToFile
88
self._encoding = 'utf-8'
99
if saveToFile is not None:
10+
print('Saving to file', saveToFile)
11+
time.sleep(1)
1012
CHUNK_SIZE = 512 # bytes
1113
with open(saveToFile, 'w') as outfile:
14+
print('Opened file', saveToFile)
15+
time.sleep(1)
1216
data = self._socket.read(CHUNK_SIZE)
1317
while data:
1418
outfile.write(data)
1519
data = self._socket.read(CHUNK_SIZE)
1620
outfile.close()
21+
print('Data written to file', saveToFile)
22+
time.sleep(1)
1723

1824
self.close()
1925

@@ -39,8 +45,14 @@ def text(self):
3945
return str(self.content, self._encoding)
4046

4147
def json(self):
42-
import ujson
43-
return ujson.loads(self.content)
48+
result = None
49+
try:
50+
import ujson
51+
result = ujson.load(self._socket)
52+
finally:
53+
self.close()
54+
return result
55+
4456

4557

4658
class HttpClient:
@@ -49,6 +61,8 @@ def __init__(self, headers={}):
4961
self._headers = headers
5062

5163
def request(self, method, url, data=None, json=None, file=None, custom=None, saveToFile=None, headers={}, stream=None):
64+
print('Req', url)
65+
5266
def _write_headers(sock, _headers):
5367
for k in _headers:
5468
sock.write(b'{}: {}\r\n'.format(k, _headers[k]))
@@ -70,18 +84,34 @@ def _write_headers(sock, _headers):
7084
host, port = host.split(':', 1)
7185
port = int(port)
7286

87+
print('Getting addr info')
88+
time.sleep(1)
7389
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
7490
if len(ai) < 1:
7591
raise ValueError('You are not connected to the internet...')
7692
ai = ai[0]
7793

78-
print(ai)
79-
94+
print('Opening socket', ai)
95+
time.sleep(1)
8096
s = usocket.socket(ai[0], ai[1], ai[2])
97+
print('Socket open')
98+
time.sleep(1)
8199
try:
100+
print('Connecting to socket')
101+
time.sleep(1)
82102
s.connect(ai[-1])
103+
print('Connected to socket')
104+
time.sleep(1)
83105
if proto == 'https:':
84-
s = ussl.wrap_socket(s, server_hostname=host)
106+
try:
107+
print('Wrapping socket with ssl')
108+
time.sleep(1)
109+
s = ussl.wrap_socket(s)
110+
print('Wrapped socket with ssl')
111+
time.sleep(1)
112+
except:
113+
print("An error occurred.")
114+
85115
s.write(b'%s /%s HTTP/1.0\r\n' % (method, path))
86116
if not 'Host' in headers:
87117
s.write(b'Host: %s\r\n' % host)

app/ota_updater.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ def _download_all_files(self, version, sub_dir=''):
140140
for file in file_list.json():
141141
path = self.modulepath(self.new_version_dir + '/' + file['path'].replace(self.main_dir + '/', '').replace(self.github_src_dir, ''))
142142
if file['type'] == 'file':
143-
download_url = file['download_url']
144-
self._download_file(download_url.replace('refs/tags/', ''), path)
143+
gitPath = file['path']
144+
print('\tDownloading: ', gitPath, 'to', path)
145+
self._download_file(version, gitPath, path)
145146
elif file['type'] == 'dir':
146147
print('Creating dir', path)
147148
self.mkdir(path)
@@ -150,13 +151,12 @@ def _download_all_files(self, version, sub_dir=''):
150151
file_list.close()
151152

152153
def _download_file(self, version, gitPath, path):
153-
print('\tDownloading: ', gitPath, 'to', path)
154154
repo_name = self.github_repo.replace('https://api.github.com/repos/', '')
155155
try:
156-
self.http_client.get('https://raw.githubusercontent.com/{}/{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
156+
self.http_client.get('https://cdn.jsdelivr.net/gh/{}@{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
157157
except OSError as err:
158158
gc.collect()
159-
self.http_client.get('https://cdn.jsdelivr.net/gh/{}@{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
159+
self.http_client.get('https://raw.githubusercontent.com/{}/{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
160160
pass
161161

162162

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22

33
def connectToWifiAndUpdate():
4-
import machine, network, gc, app.secrets as secrets
4+
import time, machine, network, gc, app.secrets as secrets
5+
time.sleep(1)
56
print('Memory free', gc.mem_free())
67

78
from app.ota_updater import OTAUpdater

0 commit comments

Comments
 (0)