Skip to content

Commit 479d306

Browse files
authored
Merge pull request #32 from Vovaman/b-31-fix-errors
B 31 fix errors
2 parents 9ce1cee + 8307857 commit 479d306

File tree

4 files changed

+25
-63
lines changed

4 files changed

+25
-63
lines changed

HISTORY.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

async_websocket_client/ws.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, ms_delay_for_read: int = 5):
3636
self._lock_for_open = a.Lock()
3737
self.sock = None
3838

39-
async def open(self, new_val: bool = None):
39+
async def open(self, new_val: bool | None = None):
4040
await self._lock_for_open.acquire()
4141
if new_val is not None:
4242
if not new_val and self.sock:
@@ -47,7 +47,9 @@ async def open(self, new_val: bool = None):
4747
self._lock_for_open.release()
4848
return to_return
4949

50-
async def close(self):
50+
async def close(self, code=None):
51+
if code is not None:
52+
print("Connection is closed. Code: ", code)
5153
return await self.open(False)
5254

5355
def urlparse(self, uri):
@@ -72,14 +74,14 @@ async def a_readline(self):
7274

7375
return line
7476

75-
async def a_read(self, size: int = None):
77+
async def a_read(self, size: int | None = None):
7678
if size == 0:
7779
return b''
7880
chunks = []
7981

8082
while True:
81-
b = self.sock.read(size)
82-
await a.sleep_ms(self.delay_read)
83+
b = self.sock.read(size) # type: ignore
84+
await a.sleep_ms(self.delay_read) # type: ignore
8385

8486
# Continue reading if the socket returns None
8587
if b is None: continue
@@ -89,7 +91,7 @@ async def a_read(self, size: int = None):
8991
if len(b) == 0: break
9092

9193
chunks.append(b)
92-
size -= len(b)
94+
size -= len(b) # type: ignore
9395

9496
# After reading the first chunk, we can break if size is None or 0
9597
if size is None or size == 0: break
@@ -99,45 +101,45 @@ async def a_read(self, size: int = None):
99101

100102
async def handshake(self, uri, headers=[], keyfile=None, certfile=None, cafile=None, cert_reqs=0):
101103
if self.sock:
102-
self.close()
104+
await self.close()
103105

104106
self.sock = socket.socket()
105107
self.uri = self.urlparse(uri)
106-
ai = socket.getaddrinfo(self.uri.hostname, self.uri.port)
108+
ai = socket.getaddrinfo(self.uri.hostname, self.uri.port) # type: ignore
107109
addr = ai[0][4]
108110

109111
self.sock.connect(addr)
110112
self.sock.setblocking(False)
111113

112-
if self.uri.protocol == 'wss':
114+
if self.uri.protocol == 'wss': # type: ignore
113115
cadata = None
114116
if not cafile is None:
115117
with open(cafile, 'rb') as f:
116118
cadata = f.read()
117119
self.sock = ssl.wrap_socket(
118120
self.sock, server_side=False,
119-
key=keyfile, cert=certfile,
121+
key=keyfile, cert=certfile, # type: ignore
120122
cert_reqs=cert_reqs, # 0 - NONE, 1 - OPTIONAL, 2 - REQUIED
121-
cadata=cadata,
122-
server_hostname=self.uri.hostname
123+
cadata=cadata, # type: ignore
124+
server_hostname=self.uri.hostname # type: ignore
123125
)
124126

125127
def send_header(header, *args):
126-
self.sock.write(header % args + '\r\n')
128+
self.sock.write(header % args + '\r\n') # type: ignore
127129

128130
# Sec-WebSocket-Key is 16 bytes of random base64 encoded
129131
key = b.b2a_base64(bytes(r.getrandbits(8)
130132
for _ in range(16)))[:-1]
131133

132-
send_header(b'GET %s HTTP/1.1', self.uri.path or '/')
133-
send_header(b'Host: %s:%s', self.uri.hostname, self.uri.port)
134+
send_header(b'GET %s HTTP/1.1', self.uri.path or '/') # type: ignore
135+
send_header(b'Host: %s:%s', self.uri.hostname, self.uri.port) # type: ignore
134136
send_header(b'Connection: Upgrade')
135137
send_header(b'Upgrade: websocket')
136138
send_header(b'Sec-WebSocket-Key: %s', key)
137139
send_header(b'Sec-WebSocket-Version: 13')
138-
send_header(b'Origin: http://{hostname}:{port}'.format(
139-
hostname=self.uri.hostname,
140-
port=self.uri.port)
140+
send_header(b'Origin: http://{hostname}:{port}'.format( # type: ignore
141+
hostname=self.uri.hostname, # type: ignore
142+
port=self.uri.port) # type: ignore
141143
)
142144

143145
for key, value in headers:
@@ -182,7 +184,7 @@ async def read_frame(self, max_size=None):
182184
data = await self.a_read(length)
183185
except MemoryError:
184186
# We can't receive this many bytes, close the socket
185-
self.close(code=CLOSE_TOO_BIG)
187+
await self.close(code=CLOSE_TOO_BIG)
186188
# await self._stream.drain()
187189
return True, OP_CLOSE, None
188190

how_to_make_package.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
> Outdated method.
2+
> Install this package using mip, as described in README.md.
3+
14
Delete previous versions first.
25

36
# build package

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "micropython-async_websocket_client"
7-
version = "0.3.4"
7+
version = "0.3.5"
88
authors = [
99
{name = "Vladimir Badashkin", email = "bd_postbox1@mail.ru"},
1010
]

0 commit comments

Comments
 (0)