@@ -36,7 +36,7 @@ def __init__(self, ms_delay_for_read: int = 5):
36
36
self ._lock_for_open = a .Lock ()
37
37
self .sock = None
38
38
39
- async def open (self , new_val : bool = None ):
39
+ async def open (self , new_val : bool | None = None ):
40
40
await self ._lock_for_open .acquire ()
41
41
if new_val is not None :
42
42
if not new_val and self .sock :
@@ -47,7 +47,9 @@ async def open(self, new_val: bool = None):
47
47
self ._lock_for_open .release ()
48
48
return to_return
49
49
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 )
51
53
return await self .open (False )
52
54
53
55
def urlparse (self , uri ):
@@ -72,14 +74,14 @@ async def a_readline(self):
72
74
73
75
return line
74
76
75
- async def a_read (self , size : int = None ):
77
+ async def a_read (self , size : int | None = None ):
76
78
if size == 0 :
77
79
return b''
78
80
chunks = []
79
81
80
82
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
83
85
84
86
# Continue reading if the socket returns None
85
87
if b is None : continue
@@ -89,7 +91,7 @@ async def a_read(self, size: int = None):
89
91
if len (b ) == 0 : break
90
92
91
93
chunks .append (b )
92
- size -= len (b )
94
+ size -= len (b ) # type: ignore
93
95
94
96
# After reading the first chunk, we can break if size is None or 0
95
97
if size is None or size == 0 : break
@@ -99,45 +101,45 @@ async def a_read(self, size: int = None):
99
101
100
102
async def handshake (self , uri , headers = [], keyfile = None , certfile = None , cafile = None , cert_reqs = 0 ):
101
103
if self .sock :
102
- self .close ()
104
+ await self .close ()
103
105
104
106
self .sock = socket .socket ()
105
107
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
107
109
addr = ai [0 ][4 ]
108
110
109
111
self .sock .connect (addr )
110
112
self .sock .setblocking (False )
111
113
112
- if self .uri .protocol == 'wss' :
114
+ if self .uri .protocol == 'wss' : # type: ignore
113
115
cadata = None
114
116
if not cafile is None :
115
117
with open (cafile , 'rb' ) as f :
116
118
cadata = f .read ()
117
119
self .sock = ssl .wrap_socket (
118
120
self .sock , server_side = False ,
119
- key = keyfile , cert = certfile ,
121
+ key = keyfile , cert = certfile , # type: ignore
120
122
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
123
125
)
124
126
125
127
def send_header (header , * args ):
126
- self .sock .write (header % args + '\r \n ' )
128
+ self .sock .write (header % args + '\r \n ' ) # type: ignore
127
129
128
130
# Sec-WebSocket-Key is 16 bytes of random base64 encoded
129
131
key = b .b2a_base64 (bytes (r .getrandbits (8 )
130
132
for _ in range (16 )))[:- 1 ]
131
133
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
134
136
send_header (b'Connection: Upgrade' )
135
137
send_header (b'Upgrade: websocket' )
136
138
send_header (b'Sec-WebSocket-Key: %s' , key )
137
139
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
141
143
)
142
144
143
145
for key , value in headers :
@@ -182,7 +184,7 @@ async def read_frame(self, max_size=None):
182
184
data = await self .a_read (length )
183
185
except MemoryError :
184
186
# 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 )
186
188
# await self._stream.drain()
187
189
return True , OP_CLOSE , None
188
190
0 commit comments