rnet 3.0.0rc3__cp311-abi3-manylinux_2_34_armv7l.whl → 3.0.0rc5__cp311-abi3-manylinux_2_34_armv7l.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rnet might be problematic. Click here for more details.
- rnet/__init__.py +1 -3
- rnet/__init__.pyi +798 -614
- rnet/blocking.py +228 -274
- rnet/cookie.py +15 -5
- rnet/emulation.py +6 -2
- rnet/exceptions.py +17 -13
- rnet/header.py +17 -3
- rnet/rnet.abi3.so +0 -0
- rnet/tls.py +5 -3
- {rnet-3.0.0rc3.dist-info → rnet-3.0.0rc5.dist-info}/METADATA +45 -46
- rnet-3.0.0rc5.dist-info/RECORD +14 -0
- rnet-3.0.0rc5.dist-info/licenses/LICENSE +201 -0
- rnet-3.0.0rc3.dist-info/RECORD +0 -14
- rnet-3.0.0rc3.dist-info/licenses/LICENSE +0 -674
- {rnet-3.0.0rc3.dist-info → rnet-3.0.0rc5.dist-info}/WHEEL +0 -0
rnet/__init__.pyi
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import datetime
|
|
1
2
|
import ipaddress
|
|
2
|
-
import typing
|
|
3
3
|
from typing import (
|
|
4
|
+
AsyncGenerator,
|
|
5
|
+
Generator,
|
|
4
6
|
Optional,
|
|
5
7
|
Tuple,
|
|
6
8
|
Union,
|
|
@@ -8,125 +10,880 @@ from typing import (
|
|
|
8
10
|
Dict,
|
|
9
11
|
List,
|
|
10
12
|
TypedDict,
|
|
13
|
+
Unpack,
|
|
14
|
+
NotRequired,
|
|
11
15
|
)
|
|
12
16
|
from pathlib import Path
|
|
13
17
|
from enum import Enum, auto
|
|
14
18
|
|
|
15
|
-
from .cookie import
|
|
16
|
-
from .
|
|
17
|
-
from .
|
|
18
|
-
from .
|
|
19
|
-
from .tls import *
|
|
19
|
+
from .cookie import Cookie, Jar
|
|
20
|
+
from .header import HeaderMap, OrigHeaderMap
|
|
21
|
+
from .emulation import Emulation, EmulationOption
|
|
22
|
+
from .tls import TlsVersion, Identity, KeyLogPolicy, CertStore
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
class Method(Enum):
|
|
25
|
+
r"""
|
|
26
|
+
An HTTP method.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
GET = auto()
|
|
30
|
+
HEAD = auto()
|
|
31
|
+
POST = auto()
|
|
32
|
+
PUT = auto()
|
|
33
|
+
DELETE = auto()
|
|
34
|
+
OPTIONS = auto()
|
|
35
|
+
TRACE = auto()
|
|
36
|
+
PATCH = auto()
|
|
37
|
+
|
|
38
|
+
class Version(Enum):
|
|
39
|
+
r"""
|
|
40
|
+
An HTTP version.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
HTTP_09 = auto()
|
|
44
|
+
HTTP_10 = auto()
|
|
45
|
+
HTTP_11 = auto()
|
|
46
|
+
HTTP_2 = auto()
|
|
47
|
+
HTTP_3 = auto()
|
|
48
|
+
|
|
49
|
+
class StatusCode:
|
|
50
|
+
r"""
|
|
51
|
+
HTTP status code.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __str__(self) -> str: ...
|
|
55
|
+
def as_int(self) -> int:
|
|
56
|
+
r"""
|
|
57
|
+
Return the status code as an integer.
|
|
58
|
+
"""
|
|
59
|
+
...
|
|
60
|
+
|
|
61
|
+
def is_informational(self) -> bool:
|
|
62
|
+
r"""
|
|
63
|
+
Check if status is within 100-199.
|
|
64
|
+
"""
|
|
65
|
+
...
|
|
66
|
+
|
|
67
|
+
def is_success(self) -> bool:
|
|
68
|
+
r"""
|
|
69
|
+
Check if status is within 200-299.
|
|
70
|
+
"""
|
|
71
|
+
...
|
|
72
|
+
|
|
73
|
+
def is_redirection(self) -> bool:
|
|
74
|
+
r"""
|
|
75
|
+
Check if status is within 300-399.
|
|
76
|
+
"""
|
|
77
|
+
...
|
|
78
|
+
|
|
79
|
+
def is_client_error(self) -> bool:
|
|
80
|
+
r"""
|
|
81
|
+
Check if status is within 400-499.
|
|
82
|
+
"""
|
|
83
|
+
...
|
|
84
|
+
|
|
85
|
+
def is_server_error(self) -> bool:
|
|
86
|
+
r"""
|
|
87
|
+
Check if status is within 500-599.
|
|
88
|
+
"""
|
|
89
|
+
...
|
|
90
|
+
|
|
91
|
+
class SocketAddr:
|
|
92
|
+
r"""
|
|
93
|
+
A IP socket address.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
def __str__(self) -> str: ...
|
|
97
|
+
def ip(self) -> Union[ipaddress.IPv4Address, ipaddress.IPv6Address]:
|
|
98
|
+
r"""
|
|
99
|
+
Returns the IP address of the socket address.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
def port(self) -> int:
|
|
103
|
+
r"""
|
|
104
|
+
Returns the port number of the socket address.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
class Multipart:
|
|
108
|
+
r"""
|
|
109
|
+
A multipart form for a request.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
def __init__(self, *parts) -> None:
|
|
113
|
+
r"""
|
|
114
|
+
Creates a new multipart form.
|
|
115
|
+
"""
|
|
116
|
+
...
|
|
117
|
+
|
|
118
|
+
class Part:
|
|
119
|
+
r"""
|
|
120
|
+
A part of a multipart form.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
def __init__(
|
|
124
|
+
self,
|
|
125
|
+
name: str,
|
|
126
|
+
value: Union[
|
|
127
|
+
str,
|
|
128
|
+
bytes,
|
|
129
|
+
Path,
|
|
130
|
+
Generator[bytes, str, None],
|
|
131
|
+
AsyncGenerator[bytes, str],
|
|
132
|
+
],
|
|
133
|
+
filename: str | None = None,
|
|
134
|
+
mime: str | None = None,
|
|
135
|
+
length: int | None = None,
|
|
136
|
+
headers: HeaderMap | None = None,
|
|
137
|
+
) -> None:
|
|
138
|
+
r"""
|
|
139
|
+
Creates a new part.
|
|
140
|
+
|
|
141
|
+
# Arguments
|
|
142
|
+
- `name` - The name of the part.
|
|
143
|
+
- `value` - The value of the part, either text, bytes, a file path, or a async or sync stream.
|
|
144
|
+
- `filename` - The filename of the part.
|
|
145
|
+
- `mime` - The MIME type of the part.
|
|
146
|
+
- `length` - The length of the part when value is a stream (e.g., for file uploads).
|
|
147
|
+
- `headers` - The custom headers for the part.
|
|
148
|
+
"""
|
|
149
|
+
...
|
|
150
|
+
|
|
151
|
+
class ProxyParams(TypedDict):
|
|
152
|
+
username: NotRequired[str]
|
|
153
|
+
r"""Username for proxy authentication."""
|
|
154
|
+
|
|
155
|
+
password: NotRequired[str]
|
|
156
|
+
r"""Password for proxy authentication."""
|
|
157
|
+
|
|
158
|
+
custom_http_auth: NotRequired[str]
|
|
159
|
+
r"""Custom HTTP proxy authentication header value."""
|
|
160
|
+
|
|
161
|
+
custom_http_headers: NotRequired[Union[Dict[str, str], HeaderMap]]
|
|
162
|
+
r"""Custom HTTP proxy headers."""
|
|
163
|
+
|
|
164
|
+
exclusion: NotRequired[str]
|
|
165
|
+
r"""List of domains to exclude from proxying."""
|
|
166
|
+
|
|
167
|
+
class Proxy:
|
|
168
|
+
r"""
|
|
169
|
+
A proxy server for a request.
|
|
170
|
+
Supports HTTP, HTTPS, SOCKS4, SOCKS4a, SOCKS5, and SOCKS5h protocols.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
@staticmethod
|
|
174
|
+
def http(url: str, **kwargs: Unpack[ProxyParams]) -> "Proxy":
|
|
175
|
+
r"""
|
|
176
|
+
Creates a new HTTP proxy.
|
|
177
|
+
|
|
178
|
+
This method sets up a proxy server for HTTP requests.
|
|
179
|
+
|
|
180
|
+
# Examples
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
import rnet
|
|
184
|
+
|
|
185
|
+
proxy = rnet.Proxy.http("http://proxy.example.com")
|
|
186
|
+
```
|
|
187
|
+
"""
|
|
188
|
+
|
|
189
|
+
@staticmethod
|
|
190
|
+
def https(url: str, **kwargs: Unpack[ProxyParams]) -> "Proxy":
|
|
191
|
+
r"""
|
|
192
|
+
Creates a new HTTPS proxy.
|
|
193
|
+
|
|
194
|
+
This method sets up a proxy server for HTTPS requests.
|
|
195
|
+
|
|
196
|
+
# Examples
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
import rnet
|
|
200
|
+
|
|
201
|
+
proxy = rnet.Proxy.https("https://proxy.example.com")
|
|
202
|
+
```
|
|
203
|
+
"""
|
|
204
|
+
|
|
205
|
+
@staticmethod
|
|
206
|
+
def all(url: str, **kwargs: Unpack[ProxyParams]) -> "Proxy":
|
|
207
|
+
r"""
|
|
208
|
+
Creates a new proxy for all protocols.
|
|
209
|
+
|
|
210
|
+
This method sets up a proxy server for all types of requests (HTTP, HTTPS, etc.).
|
|
211
|
+
|
|
212
|
+
# Examples
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
import rnet
|
|
216
|
+
|
|
217
|
+
proxy = rnet.Proxy.all("https://proxy.example.com")
|
|
218
|
+
```
|
|
219
|
+
"""
|
|
220
|
+
|
|
221
|
+
class Message:
|
|
222
|
+
r"""
|
|
223
|
+
A WebSocket message.
|
|
224
|
+
"""
|
|
225
|
+
|
|
226
|
+
data: Optional[bytes]
|
|
227
|
+
r"""
|
|
228
|
+
Returns the data of the message as bytes.
|
|
229
|
+
"""
|
|
230
|
+
|
|
231
|
+
text: Optional[str]
|
|
232
|
+
r"""
|
|
233
|
+
Returns the text content of the message if it is a text message.
|
|
234
|
+
"""
|
|
235
|
+
|
|
236
|
+
binary: Optional[bytes]
|
|
237
|
+
r"""
|
|
238
|
+
Returns the binary data of the message if it is a binary message.
|
|
239
|
+
"""
|
|
240
|
+
|
|
241
|
+
ping: Optional[bytes]
|
|
242
|
+
r"""
|
|
243
|
+
Returns the ping data of the message if it is a ping message.
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
pong: Optional[bytes]
|
|
247
|
+
r"""
|
|
248
|
+
Returns the pong data of the message if it is a pong message.
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
close: Optional[Tuple[int, Optional[str]]]
|
|
252
|
+
r"""
|
|
253
|
+
Returns the close code and reason of the message if it is a close message.
|
|
254
|
+
"""
|
|
255
|
+
|
|
256
|
+
@staticmethod
|
|
257
|
+
def text_from_json(json: Dict[str, Any]) -> "Message":
|
|
258
|
+
r"""
|
|
259
|
+
Creates a new text message from the JSON representation.
|
|
260
|
+
|
|
261
|
+
# Arguments
|
|
262
|
+
* `json` - The JSON representation of the message.
|
|
263
|
+
"""
|
|
264
|
+
...
|
|
265
|
+
|
|
266
|
+
@staticmethod
|
|
267
|
+
def binary_from_json(json: Dict[str, Any]) -> "Message":
|
|
268
|
+
r"""
|
|
269
|
+
Creates a new binary message from the JSON representation.
|
|
270
|
+
|
|
271
|
+
# Arguments
|
|
272
|
+
* `json` - The JSON representation of the message.
|
|
273
|
+
"""
|
|
274
|
+
|
|
275
|
+
@staticmethod
|
|
276
|
+
def from_text(text: str) -> "Message":
|
|
277
|
+
r"""
|
|
278
|
+
Creates a new text message.
|
|
279
|
+
|
|
280
|
+
# Arguments
|
|
281
|
+
|
|
282
|
+
* `text` - The text content of the message.
|
|
283
|
+
"""
|
|
284
|
+
|
|
285
|
+
@staticmethod
|
|
286
|
+
def from_binary(data: bytes) -> "Message":
|
|
287
|
+
r"""
|
|
288
|
+
Creates a new binary message.
|
|
289
|
+
|
|
290
|
+
# Arguments
|
|
291
|
+
|
|
292
|
+
* `data` - The binary data of the message.
|
|
293
|
+
"""
|
|
294
|
+
|
|
295
|
+
@staticmethod
|
|
296
|
+
def from_ping(data: bytes) -> "Message":
|
|
297
|
+
r"""
|
|
298
|
+
Creates a new ping message.
|
|
299
|
+
|
|
300
|
+
# Arguments
|
|
301
|
+
|
|
302
|
+
* `data` - The ping data of the message.
|
|
303
|
+
"""
|
|
304
|
+
|
|
305
|
+
@staticmethod
|
|
306
|
+
def from_pong(data: bytes) -> "Message":
|
|
307
|
+
r"""
|
|
308
|
+
Creates a new pong message.
|
|
309
|
+
|
|
310
|
+
# Arguments
|
|
311
|
+
|
|
312
|
+
* `data` - The pong data of the message.
|
|
313
|
+
"""
|
|
314
|
+
|
|
315
|
+
@staticmethod
|
|
316
|
+
def from_close(code: int, reason: str | None = None) -> "Message":
|
|
317
|
+
r"""
|
|
318
|
+
Creates a new close message.
|
|
319
|
+
|
|
320
|
+
# Arguments
|
|
321
|
+
|
|
322
|
+
* `code` - The close code.
|
|
323
|
+
* `reason` - An optional reason for closing.
|
|
324
|
+
"""
|
|
325
|
+
|
|
326
|
+
def json(self) -> Dict[str, Any]:
|
|
327
|
+
r"""
|
|
328
|
+
Returns the JSON representation of the message.
|
|
329
|
+
"""
|
|
330
|
+
|
|
331
|
+
def __str__(self) -> str: ...
|
|
332
|
+
|
|
333
|
+
class History:
|
|
334
|
+
"""
|
|
335
|
+
An entry in the redirect history.
|
|
336
|
+
"""
|
|
337
|
+
|
|
338
|
+
status: int
|
|
339
|
+
"""Get the status code of the redirect response."""
|
|
340
|
+
|
|
341
|
+
url: str
|
|
342
|
+
"""Get the URL of the redirect response."""
|
|
343
|
+
|
|
344
|
+
previous: str
|
|
345
|
+
"""Get the previous URL before the redirect response."""
|
|
346
|
+
|
|
347
|
+
headers: HeaderMap
|
|
348
|
+
"""Get the headers of the redirect response."""
|
|
349
|
+
|
|
350
|
+
def __str__(self) -> str: ...
|
|
351
|
+
|
|
352
|
+
class Streamer:
|
|
353
|
+
r"""
|
|
354
|
+
A byte stream response.
|
|
355
|
+
An asynchronous iterator yielding data chunks from the response stream.
|
|
356
|
+
Used to stream response content.
|
|
357
|
+
Implemented in the `stream` method of the `Response` class.
|
|
358
|
+
Can be used in an asynchronous for loop in Python.
|
|
359
|
+
|
|
360
|
+
# Examples
|
|
361
|
+
|
|
362
|
+
```python
|
|
363
|
+
import asyncio
|
|
364
|
+
import rnet
|
|
365
|
+
from rnet import Method, Emulation
|
|
366
|
+
|
|
367
|
+
async def main():
|
|
368
|
+
resp = await rnet.get("https://httpbin.org/stream/20")
|
|
369
|
+
async with resp.stream() as streamer:
|
|
370
|
+
async for chunk in streamer:
|
|
371
|
+
print("Chunk: ", chunk)
|
|
372
|
+
await asyncio.sleep(0.1)
|
|
373
|
+
|
|
374
|
+
if __name__ == "__main__":
|
|
375
|
+
asyncio.run(main())
|
|
376
|
+
```
|
|
377
|
+
"""
|
|
378
|
+
|
|
379
|
+
async def __aiter__(self) -> "Streamer": ...
|
|
380
|
+
async def __anext__(self) -> Optional[bytes]: ...
|
|
381
|
+
async def __aenter__(self) -> Any: ...
|
|
382
|
+
async def __aexit__(
|
|
383
|
+
self, _exc_type: Any, _exc_value: Any, _traceback: Any
|
|
384
|
+
) -> Any: ...
|
|
385
|
+
def __iter__(self) -> "Streamer": ...
|
|
386
|
+
def __next__(self) -> bytes: ...
|
|
387
|
+
def __enter__(self) -> "Streamer": ...
|
|
388
|
+
def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> None: ...
|
|
389
|
+
|
|
390
|
+
class Response:
|
|
391
|
+
r"""
|
|
392
|
+
A response from a request.
|
|
393
|
+
|
|
394
|
+
# Examples
|
|
395
|
+
|
|
396
|
+
```python
|
|
397
|
+
import asyncio
|
|
398
|
+
import rnet
|
|
399
|
+
|
|
400
|
+
async def main():
|
|
401
|
+
response = await rnet.get("https://www.rust-lang.org")
|
|
402
|
+
print("Status Code: ", response.status)
|
|
403
|
+
print("Version: ", response.version)
|
|
404
|
+
print("Response URL: ", response.url)
|
|
405
|
+
print("Headers: ", response.headers)
|
|
406
|
+
print("Content-Length: ", response.content_length)
|
|
407
|
+
print("Encoding: ", response.encoding)
|
|
408
|
+
print("Remote Address: ", response.remote_addr)
|
|
409
|
+
|
|
410
|
+
text_content = await response.text()
|
|
411
|
+
print("Text: ", text_content)
|
|
412
|
+
|
|
413
|
+
if __name__ == "__main__":
|
|
414
|
+
asyncio.run(main())
|
|
415
|
+
```
|
|
416
|
+
"""
|
|
417
|
+
|
|
418
|
+
url: str
|
|
419
|
+
r"""
|
|
420
|
+
Get the URL of the response.
|
|
421
|
+
"""
|
|
422
|
+
|
|
423
|
+
status: StatusCode
|
|
424
|
+
r"""
|
|
425
|
+
Get the status code of the response.
|
|
426
|
+
"""
|
|
427
|
+
|
|
428
|
+
version: Version
|
|
429
|
+
r"""
|
|
430
|
+
Get the HTTP version of the response.
|
|
431
|
+
"""
|
|
432
|
+
|
|
433
|
+
headers: HeaderMap
|
|
434
|
+
r"""
|
|
435
|
+
Get the headers of the response.
|
|
436
|
+
"""
|
|
437
|
+
|
|
438
|
+
cookies: List[Cookie]
|
|
439
|
+
r"""
|
|
440
|
+
Get the cookies of the response.
|
|
441
|
+
"""
|
|
442
|
+
|
|
443
|
+
content_length: Optional[int]
|
|
444
|
+
r"""
|
|
445
|
+
Get the content length of the response.
|
|
446
|
+
"""
|
|
447
|
+
|
|
448
|
+
remote_addr: Optional[SocketAddr]
|
|
449
|
+
r"""
|
|
450
|
+
Get the remote address of the response.
|
|
451
|
+
"""
|
|
452
|
+
|
|
453
|
+
local_addr: Optional[SocketAddr]
|
|
454
|
+
r"""
|
|
455
|
+
Get the local address of the response.
|
|
456
|
+
"""
|
|
457
|
+
|
|
458
|
+
history: List[History]
|
|
459
|
+
r"""
|
|
460
|
+
Get the redirect history of the Response.
|
|
461
|
+
"""
|
|
462
|
+
|
|
463
|
+
peer_certificate: Optional[bytes]
|
|
464
|
+
r"""
|
|
465
|
+
Get the DER encoded leaf certificate of the response.
|
|
466
|
+
"""
|
|
467
|
+
|
|
468
|
+
async def text(self) -> str:
|
|
469
|
+
r"""
|
|
470
|
+
Get the text content of the response.
|
|
471
|
+
"""
|
|
472
|
+
|
|
473
|
+
async def text_with_charset(self, encoding: str) -> str:
|
|
474
|
+
r"""
|
|
475
|
+
Get the full response text given a specific encoding.
|
|
476
|
+
"""
|
|
477
|
+
|
|
478
|
+
async def json(self) -> Any:
|
|
479
|
+
r"""
|
|
480
|
+
Get the JSON content of the response.
|
|
481
|
+
"""
|
|
482
|
+
|
|
483
|
+
async def bytes(self) -> bytes:
|
|
484
|
+
r"""
|
|
485
|
+
Get the bytes content of the response.
|
|
486
|
+
"""
|
|
487
|
+
|
|
488
|
+
def stream(self) -> Streamer:
|
|
489
|
+
r"""
|
|
490
|
+
Get the response into a `Stream` of `Bytes` from the body.
|
|
491
|
+
"""
|
|
492
|
+
|
|
493
|
+
async def close(self) -> None:
|
|
494
|
+
r"""
|
|
495
|
+
Close the response connection.
|
|
496
|
+
"""
|
|
497
|
+
|
|
498
|
+
async def __aenter__(self) -> Any: ...
|
|
499
|
+
async def __aexit__(
|
|
500
|
+
self, _exc_type: Any, _exc_value: Any, _traceback: Any
|
|
501
|
+
) -> Any: ...
|
|
502
|
+
|
|
503
|
+
class WebSocket:
|
|
504
|
+
r"""
|
|
505
|
+
A WebSocket response.
|
|
506
|
+
"""
|
|
507
|
+
|
|
508
|
+
status: StatusCode
|
|
509
|
+
r"""
|
|
510
|
+
Get the status code of the response.
|
|
511
|
+
"""
|
|
512
|
+
|
|
513
|
+
version: Version
|
|
514
|
+
r"""
|
|
515
|
+
Get the HTTP version of the response.
|
|
516
|
+
"""
|
|
517
|
+
|
|
518
|
+
headers: HeaderMap
|
|
519
|
+
r"""
|
|
520
|
+
Get the headers of the response.
|
|
521
|
+
"""
|
|
522
|
+
|
|
523
|
+
cookies: List[Cookie]
|
|
524
|
+
r"""
|
|
525
|
+
Get the cookies of the response.
|
|
526
|
+
"""
|
|
527
|
+
|
|
528
|
+
remote_addr: Optional[SocketAddr]
|
|
529
|
+
r"""
|
|
530
|
+
Get the remote address of the response.
|
|
531
|
+
"""
|
|
532
|
+
|
|
533
|
+
protocol: Optional[str]
|
|
534
|
+
r"""
|
|
535
|
+
Get the WebSocket protocol.
|
|
536
|
+
"""
|
|
537
|
+
|
|
538
|
+
async def recv(
|
|
539
|
+
self, timeout: datetime.timedelta | None = None
|
|
540
|
+
) -> Optional[Message]:
|
|
541
|
+
r"""
|
|
542
|
+
Receive a message from the WebSocket.
|
|
543
|
+
"""
|
|
544
|
+
|
|
545
|
+
async def send(self, message: Message) -> None:
|
|
546
|
+
r"""
|
|
547
|
+
Send a message to the WebSocket.
|
|
548
|
+
|
|
549
|
+
# Arguments
|
|
550
|
+
|
|
551
|
+
* `message` - The message to send.
|
|
552
|
+
"""
|
|
25
553
|
|
|
26
|
-
|
|
554
|
+
async def send_all(self, messages: List[Message]) -> None:
|
|
555
|
+
r"""
|
|
556
|
+
Send multiple messages to the WebSocket.
|
|
557
|
+
|
|
558
|
+
# Arguments
|
|
559
|
+
|
|
560
|
+
* `messages` - The list of messages to send.
|
|
561
|
+
"""
|
|
562
|
+
|
|
563
|
+
async def close(
|
|
564
|
+
self,
|
|
565
|
+
code: int | None = None,
|
|
566
|
+
reason: str | None = None,
|
|
567
|
+
) -> None:
|
|
568
|
+
r"""
|
|
569
|
+
Close the WebSocket connection.
|
|
570
|
+
|
|
571
|
+
# Arguments
|
|
572
|
+
|
|
573
|
+
* `code` - An optional close code.
|
|
574
|
+
* `reason` - An optional reason for closing.
|
|
575
|
+
"""
|
|
576
|
+
|
|
577
|
+
def __aenter__(self) -> Any: ...
|
|
578
|
+
def __aexit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> Any: ...
|
|
579
|
+
|
|
580
|
+
class ClientParams(TypedDict):
|
|
27
581
|
emulation: NotRequired[Union[Emulation, EmulationOption]]
|
|
582
|
+
"""Browser fingerprint/Emulation config."""
|
|
583
|
+
|
|
28
584
|
user_agent: NotRequired[str]
|
|
585
|
+
"""Default User-Agent string."""
|
|
586
|
+
|
|
29
587
|
headers: NotRequired[Union[Dict[str, str], HeaderMap]]
|
|
588
|
+
"""Default request headers."""
|
|
589
|
+
|
|
30
590
|
orig_headers: NotRequired[Union[List[str], OrigHeaderMap]]
|
|
591
|
+
"""Original request headers (case-sensitive and order)."""
|
|
592
|
+
|
|
31
593
|
referer: NotRequired[bool]
|
|
594
|
+
"""Automatically set Referer."""
|
|
595
|
+
|
|
32
596
|
history: NotRequired[bool]
|
|
597
|
+
"""Store redirect history."""
|
|
598
|
+
|
|
33
599
|
allow_redirects: NotRequired[bool]
|
|
600
|
+
"""Allow automatic redirects."""
|
|
601
|
+
|
|
34
602
|
max_redirects: NotRequired[int]
|
|
603
|
+
"""Maximum number of redirects."""
|
|
604
|
+
|
|
35
605
|
cookie_store: NotRequired[bool]
|
|
606
|
+
"""Enable cookie store."""
|
|
607
|
+
|
|
36
608
|
cookie_provider: NotRequired[Jar]
|
|
609
|
+
"""Custom cookie provider."""
|
|
610
|
+
|
|
611
|
+
lookup_ip_strategy: NotRequired[str]
|
|
612
|
+
"""IP lookup strategy."""
|
|
613
|
+
|
|
37
614
|
timeout: NotRequired[int]
|
|
615
|
+
"""Total timeout (seconds)."""
|
|
616
|
+
|
|
38
617
|
connect_timeout: NotRequired[int]
|
|
618
|
+
"""Connection timeout (seconds)."""
|
|
619
|
+
|
|
39
620
|
read_timeout: NotRequired[int]
|
|
621
|
+
"""Read timeout (seconds)."""
|
|
622
|
+
|
|
40
623
|
tcp_keepalive: NotRequired[int]
|
|
624
|
+
"""TCP keepalive time (seconds)."""
|
|
625
|
+
|
|
41
626
|
tcp_keepalive_interval: NotRequired[int]
|
|
627
|
+
"""TCP keepalive interval (seconds)."""
|
|
628
|
+
|
|
42
629
|
tcp_keepalive_retries: NotRequired[int]
|
|
630
|
+
"""TCP keepalive retry count."""
|
|
631
|
+
|
|
43
632
|
tcp_user_timeout: NotRequired[int]
|
|
633
|
+
"""TCP user timeout (seconds)."""
|
|
634
|
+
|
|
44
635
|
tcp_nodelay: NotRequired[bool]
|
|
636
|
+
"""Enable TCP_NODELAY."""
|
|
637
|
+
|
|
45
638
|
tcp_reuse_address: NotRequired[bool]
|
|
639
|
+
"""Enable SO_REUSEADDR."""
|
|
640
|
+
|
|
46
641
|
pool_idle_timeout: NotRequired[int]
|
|
642
|
+
"""Connection pool idle timeout (seconds)."""
|
|
643
|
+
|
|
47
644
|
pool_max_idle_per_host: NotRequired[int]
|
|
645
|
+
"""Max idle connections per host."""
|
|
646
|
+
|
|
48
647
|
pool_max_size: NotRequired[int]
|
|
648
|
+
"""Max total connections in pool."""
|
|
649
|
+
|
|
49
650
|
http1_only: NotRequired[bool]
|
|
651
|
+
"""Enable HTTP/1.1 only."""
|
|
652
|
+
|
|
50
653
|
http2_only: NotRequired[bool]
|
|
654
|
+
"""Enable HTTP/2 only."""
|
|
655
|
+
|
|
51
656
|
https_only: NotRequired[bool]
|
|
52
|
-
|
|
657
|
+
"""Enable HTTPS only."""
|
|
658
|
+
|
|
53
659
|
verify: NotRequired[Union[bool, Path, CertStore]]
|
|
660
|
+
"""Verify SSL or specify CA path."""
|
|
661
|
+
|
|
54
662
|
identity: NotRequired[Identity]
|
|
663
|
+
"""Represents a private key and X509 cert as a client certificate."""
|
|
664
|
+
|
|
55
665
|
keylog: NotRequired[KeyLogPolicy]
|
|
666
|
+
"""Key logging policy (environment or file)."""
|
|
667
|
+
|
|
56
668
|
tls_info: NotRequired[bool]
|
|
669
|
+
"""Return TLS info."""
|
|
670
|
+
|
|
57
671
|
min_tls_version: NotRequired[TlsVersion]
|
|
672
|
+
"""Minimum TLS version."""
|
|
673
|
+
|
|
58
674
|
max_tls_version: NotRequired[TlsVersion]
|
|
675
|
+
"""Maximum TLS version."""
|
|
676
|
+
|
|
59
677
|
no_proxy: NotRequired[bool]
|
|
678
|
+
"""Disable proxy."""
|
|
679
|
+
|
|
60
680
|
proxies: NotRequired[List[Proxy]]
|
|
681
|
+
"""Proxy server list."""
|
|
682
|
+
|
|
61
683
|
local_address: NotRequired[Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address]]
|
|
684
|
+
"""Local bind address."""
|
|
685
|
+
|
|
62
686
|
interface: NotRequired[str]
|
|
687
|
+
"""Local network interface."""
|
|
688
|
+
|
|
63
689
|
gzip: NotRequired[bool]
|
|
690
|
+
"""Enable gzip decompression."""
|
|
691
|
+
|
|
64
692
|
brotli: NotRequired[bool]
|
|
693
|
+
"""Enable brotli decompression."""
|
|
694
|
+
|
|
65
695
|
deflate: NotRequired[bool]
|
|
66
|
-
|
|
696
|
+
"""Enable deflate decompression."""
|
|
67
697
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
password: NotRequired[str]
|
|
71
|
-
custom_http_auth: NotRequired[str]
|
|
72
|
-
custom_http_headers: NotRequired[Union[Dict[str, str], HeaderMap]]
|
|
73
|
-
exclusion: NotRequired[str]
|
|
698
|
+
zstd: NotRequired[bool]
|
|
699
|
+
"""Enable zstd decompression."""
|
|
74
700
|
|
|
75
|
-
class Request(TypedDict
|
|
701
|
+
class Request(TypedDict):
|
|
76
702
|
emulation: NotRequired[Union[Emulation, EmulationOption]]
|
|
703
|
+
"""The Emulation settings for the request."""
|
|
704
|
+
|
|
77
705
|
proxy: NotRequired[Proxy]
|
|
706
|
+
"""The proxy to use for the request."""
|
|
707
|
+
|
|
78
708
|
local_address: NotRequired[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]
|
|
709
|
+
"""Bind to a local IP Address."""
|
|
710
|
+
|
|
79
711
|
interface: NotRequired[str]
|
|
712
|
+
"""Bind to an interface by SO_BINDTODEVICE."""
|
|
713
|
+
|
|
80
714
|
timeout: NotRequired[int]
|
|
715
|
+
"""The timeout to use for the request."""
|
|
716
|
+
|
|
81
717
|
read_timeout: NotRequired[int]
|
|
718
|
+
"""The read timeout to use for the request."""
|
|
719
|
+
|
|
82
720
|
version: NotRequired[Version]
|
|
721
|
+
"""The HTTP version to use for the request."""
|
|
722
|
+
|
|
83
723
|
headers: NotRequired[Union[Dict[str, str], HeaderMap]]
|
|
724
|
+
"""The headers to use for the request."""
|
|
725
|
+
|
|
84
726
|
orig_headers: NotRequired[Union[List[str], OrigHeaderMap]]
|
|
727
|
+
"""The original headers to use for the request."""
|
|
728
|
+
|
|
85
729
|
default_headers: NotRequired[bool]
|
|
730
|
+
"""The option enables default headers."""
|
|
731
|
+
|
|
86
732
|
cookies: NotRequired[Dict[str, str]]
|
|
733
|
+
"""The cookies to use for the request."""
|
|
734
|
+
|
|
87
735
|
allow_redirects: NotRequired[bool]
|
|
736
|
+
"""Whether to allow redirects."""
|
|
737
|
+
|
|
88
738
|
max_redirects: NotRequired[int]
|
|
739
|
+
"""The maximum number of redirects to follow."""
|
|
740
|
+
|
|
89
741
|
gzip: NotRequired[bool]
|
|
742
|
+
"""Sets gzip as an accepted encoding."""
|
|
743
|
+
|
|
90
744
|
brotli: NotRequired[bool]
|
|
745
|
+
"""Sets brotli as an accepted encoding."""
|
|
746
|
+
|
|
91
747
|
deflate: NotRequired[bool]
|
|
748
|
+
"""Sets deflate as an accepted encoding."""
|
|
749
|
+
|
|
92
750
|
zstd: NotRequired[bool]
|
|
751
|
+
"""Sets zstd as an accepted encoding."""
|
|
752
|
+
|
|
93
753
|
auth: NotRequired[str]
|
|
754
|
+
"""The authentication to use for the request."""
|
|
755
|
+
|
|
94
756
|
bearer_auth: NotRequired[str]
|
|
757
|
+
"""The bearer authentication to use for the request."""
|
|
758
|
+
|
|
95
759
|
basic_auth: NotRequired[Tuple[str, Optional[str]]]
|
|
760
|
+
"""The basic authentication to use for the request."""
|
|
761
|
+
|
|
96
762
|
query: NotRequired[List[Tuple[str, str]]]
|
|
763
|
+
"""The query parameters to use for the request."""
|
|
764
|
+
|
|
97
765
|
form: NotRequired[List[Tuple[str, str]]]
|
|
766
|
+
"""The form parameters to use for the request."""
|
|
767
|
+
|
|
98
768
|
json: NotRequired[Dict[str, Any]]
|
|
769
|
+
"""The JSON body to use for the request."""
|
|
770
|
+
|
|
99
771
|
body: NotRequired[
|
|
100
772
|
Union[
|
|
101
773
|
str,
|
|
102
774
|
bytes,
|
|
103
|
-
|
|
104
|
-
|
|
775
|
+
Generator[bytes, str, None],
|
|
776
|
+
AsyncGenerator[bytes, str],
|
|
105
777
|
]
|
|
106
778
|
]
|
|
779
|
+
"""The body to use for the request."""
|
|
780
|
+
|
|
107
781
|
multipart: NotRequired[Multipart]
|
|
782
|
+
"""The multipart form to use for the request."""
|
|
108
783
|
|
|
109
|
-
class WebSocketRequest(TypedDict
|
|
784
|
+
class WebSocketRequest(TypedDict):
|
|
110
785
|
emulation: NotRequired[Union[Emulation, EmulationOption]]
|
|
786
|
+
"""The Emulation settings for the request."""
|
|
787
|
+
|
|
111
788
|
proxy: NotRequired[Proxy]
|
|
789
|
+
"""The proxy to use for the request."""
|
|
790
|
+
|
|
112
791
|
local_address: NotRequired[Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address]]
|
|
792
|
+
"""Bind to a local IP Address."""
|
|
793
|
+
|
|
113
794
|
interface: NotRequired[str]
|
|
795
|
+
"""Bind to an interface by SO_BINDTODEVICE."""
|
|
796
|
+
|
|
114
797
|
headers: NotRequired[Union[Dict[str, str], HeaderMap]]
|
|
798
|
+
"""The headers to use for the request."""
|
|
799
|
+
|
|
115
800
|
orig_headers: NotRequired[Union[List[str], OrigHeaderMap]]
|
|
801
|
+
"""The original headers to use for the request."""
|
|
802
|
+
|
|
116
803
|
default_headers: NotRequired[bool]
|
|
804
|
+
"""The option enables default headers."""
|
|
805
|
+
|
|
117
806
|
cookies: NotRequired[Dict[str, str]]
|
|
807
|
+
"""The cookies to use for the request."""
|
|
808
|
+
|
|
118
809
|
protocols: NotRequired[List[str]]
|
|
810
|
+
"""The protocols to use for the request."""
|
|
811
|
+
|
|
119
812
|
force_http2: NotRequired[bool]
|
|
813
|
+
"""Whether to use HTTP/2 for the websocket."""
|
|
814
|
+
|
|
120
815
|
auth: NotRequired[str]
|
|
816
|
+
"""The authentication to use for the request."""
|
|
817
|
+
|
|
121
818
|
bearer_auth: NotRequired[str]
|
|
819
|
+
"""The bearer authentication to use for the request."""
|
|
820
|
+
|
|
122
821
|
basic_auth: NotRequired[Tuple[str, Optional[str]]]
|
|
822
|
+
"""The basic authentication to use for the request."""
|
|
823
|
+
|
|
123
824
|
query: NotRequired[List[Tuple[str, str]]]
|
|
825
|
+
"""The query parameters to use for the request."""
|
|
826
|
+
|
|
124
827
|
read_buffer_size: NotRequired[int]
|
|
828
|
+
"""
|
|
829
|
+
Read buffer capacity. This buffer is eagerly allocated and used for receiving messages.
|
|
830
|
+
|
|
831
|
+
For high read load scenarios a larger buffer, e.g. 128 KiB, improves performance.
|
|
832
|
+
|
|
833
|
+
For scenarios where you expect a lot of connections and don't need high read load
|
|
834
|
+
performance a smaller buffer, e.g. 4 KiB, would be appropriate to lower total
|
|
835
|
+
memory usage.
|
|
836
|
+
|
|
837
|
+
The default value is 128 KiB.
|
|
838
|
+
"""
|
|
839
|
+
|
|
125
840
|
write_buffer_size: NotRequired[int]
|
|
841
|
+
"""
|
|
842
|
+
The target minimum size of the write buffer to reach before writing the data
|
|
843
|
+
to the underlying stream. The default value is 128 KiB.
|
|
844
|
+
|
|
845
|
+
If set to 0 each message will be eagerly written to the underlying stream.
|
|
846
|
+
It is often more optimal to allow them to buffer a little, hence the default value.
|
|
847
|
+
|
|
848
|
+
Note: flush() will always fully write the buffer regardless.
|
|
849
|
+
"""
|
|
850
|
+
|
|
126
851
|
max_write_buffer_size: NotRequired[int]
|
|
852
|
+
"""
|
|
853
|
+
The max size of the write buffer in bytes. Setting this can provide backpressure
|
|
854
|
+
in the case the write buffer is filling up due to write errors.
|
|
855
|
+
The default value is unlimited.
|
|
856
|
+
|
|
857
|
+
Note: The write buffer only builds up past write_buffer_size when writes to the
|
|
858
|
+
underlying stream are failing. So the write buffer can not fill up if you are not
|
|
859
|
+
observing write errors even if not flushing.
|
|
860
|
+
|
|
861
|
+
Note: Should always be at least write_buffer_size + 1 message and probably a little
|
|
862
|
+
more depending on error handling strategy.
|
|
863
|
+
"""
|
|
864
|
+
|
|
127
865
|
max_message_size: NotRequired[int]
|
|
866
|
+
"""
|
|
867
|
+
The maximum size of an incoming message. None means no size limit.
|
|
868
|
+
The default value is 64 MiB which should be reasonably big for all normal use-cases
|
|
869
|
+
but small enough to prevent memory eating by a malicious user.
|
|
870
|
+
"""
|
|
871
|
+
|
|
128
872
|
max_frame_size: NotRequired[int]
|
|
873
|
+
"""
|
|
874
|
+
The maximum size of a single incoming message frame. None means no size limit.
|
|
875
|
+
The limit is for frame payload NOT including the frame header.
|
|
876
|
+
The default value is 16 MiB which should be reasonably big for all normal use-cases
|
|
877
|
+
but small enough to prevent memory eating by a malicious user.
|
|
878
|
+
"""
|
|
879
|
+
|
|
129
880
|
accept_unmasked_frames: NotRequired[bool]
|
|
881
|
+
"""
|
|
882
|
+
When set to True, the server will accept and handle unmasked frames from the client.
|
|
883
|
+
According to RFC 6455, the server must close the connection to the client in such cases,
|
|
884
|
+
however it seems like there are some popular libraries that are sending unmasked frames,
|
|
885
|
+
ignoring the RFC. By default this option is set to False, i.e. according to RFC6455.
|
|
886
|
+
"""
|
|
130
887
|
|
|
131
888
|
class Client:
|
|
132
889
|
r"""
|
|
@@ -134,67 +891,30 @@ class Client:
|
|
|
134
891
|
"""
|
|
135
892
|
|
|
136
893
|
def __init__(
|
|
137
|
-
|
|
894
|
+
self,
|
|
138
895
|
**kwargs: Unpack[ClientParams],
|
|
139
|
-
) ->
|
|
896
|
+
) -> None:
|
|
140
897
|
r"""
|
|
141
898
|
Creates a new Client instance.
|
|
142
899
|
|
|
143
|
-
Args:
|
|
144
|
-
emulation: Browser fingerprint/Emulation config.
|
|
145
|
-
user_agent: Default User-Agent string.
|
|
146
|
-
headers: Default request headers.
|
|
147
|
-
orig_headers: Original request headers (case-sensitive and order).
|
|
148
|
-
referer: Automatically set Referer.
|
|
149
|
-
allow_redirects: Allow automatic redirects.
|
|
150
|
-
max_redirects: Maximum number of redirects.
|
|
151
|
-
cookie_store: Enable cookie store.
|
|
152
|
-
lookup_ip_strategy: IP lookup strategy.
|
|
153
|
-
timeout: Total timeout (seconds).
|
|
154
|
-
connect_timeout: Connection timeout (seconds).
|
|
155
|
-
read_timeout: Read timeout (seconds).
|
|
156
|
-
tcp_keepalive: TCP keepalive time (seconds).
|
|
157
|
-
tcp_keepalive_interval: TCP keepalive interval (seconds).
|
|
158
|
-
tcp_keepalive_retries: TCP keepalive retry count.
|
|
159
|
-
tcp_user_timeout: TCP user timeout (seconds).
|
|
160
|
-
tcp_nodelay: Enable TCP_NODELAY.
|
|
161
|
-
tcp_reuse_address: Enable SO_REUSEADDR.
|
|
162
|
-
pool_idle_timeout: Connection pool idle timeout (seconds).
|
|
163
|
-
pool_max_idle_per_host: Max idle connections per host.
|
|
164
|
-
pool_max_size: Max total connections in pool.
|
|
165
|
-
http1_only: Enable HTTP/1.1 only.
|
|
166
|
-
http2_only: Enable HTTP/2 only.
|
|
167
|
-
https_only: Enable HTTPS only.
|
|
168
|
-
http2_max_retry_count: Max HTTP/2 retry count.
|
|
169
|
-
verify: Verify SSL or specify CA path.
|
|
170
|
-
identity: Represents a private key and X509 cert as a client certificate.
|
|
171
|
-
keylog: Key logging policy (environment or file).
|
|
172
|
-
tls_info: Return TLS info.
|
|
173
|
-
min_tls_version: Minimum TLS version.
|
|
174
|
-
max_tls_version: Maximum TLS version.
|
|
175
|
-
no_proxy: Disable proxy.
|
|
176
|
-
proxies: Proxy server list.
|
|
177
|
-
local_address: Local bind address.
|
|
178
|
-
interface: Local network interface.
|
|
179
|
-
gzip: Enable gzip decompression.
|
|
180
|
-
brotli: Enable brotli decompression.
|
|
181
|
-
deflate: Enable deflate decompression.
|
|
182
|
-
zstd: Enable zstd decompression.
|
|
183
|
-
|
|
184
900
|
Examples:
|
|
185
901
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
902
|
+
```python
|
|
903
|
+
import asyncio
|
|
904
|
+
import rnet
|
|
189
905
|
|
|
906
|
+
async def main():
|
|
190
907
|
client = rnet.Client(
|
|
191
|
-
user_agent="
|
|
908
|
+
user_agent="Mozilla/5.0",
|
|
192
909
|
timeout=10,
|
|
193
910
|
)
|
|
194
911
|
response = await client.get('https://httpbin.org/get')
|
|
195
|
-
print(response.text)
|
|
196
|
-
|
|
912
|
+
print(await response.text())
|
|
913
|
+
|
|
914
|
+
asyncio.run(main())
|
|
915
|
+
```
|
|
197
916
|
"""
|
|
917
|
+
...
|
|
198
918
|
|
|
199
919
|
async def request(
|
|
200
920
|
self,
|
|
@@ -430,278 +1150,14 @@ class Client:
|
|
|
430
1150
|
import asyncio
|
|
431
1151
|
from rnet import Method
|
|
432
1152
|
|
|
433
|
-
async def main():
|
|
434
|
-
client = rnet.Client()
|
|
435
|
-
response = await client.get("https://httpbin.org/anything")
|
|
436
|
-
print(await response.text())
|
|
437
|
-
|
|
438
|
-
asyncio.run(main())
|
|
439
|
-
```
|
|
440
|
-
"""
|
|
441
|
-
|
|
442
|
-
class Multipart:
|
|
443
|
-
r"""
|
|
444
|
-
A multipart form for a request.
|
|
445
|
-
"""
|
|
446
|
-
|
|
447
|
-
def __init__(cls, *parts) -> Multipart:
|
|
448
|
-
r"""
|
|
449
|
-
Creates a new multipart form.
|
|
450
|
-
"""
|
|
451
|
-
|
|
452
|
-
class Part:
|
|
453
|
-
r"""
|
|
454
|
-
A part of a multipart form.
|
|
455
|
-
"""
|
|
456
|
-
|
|
457
|
-
def __init__(
|
|
458
|
-
cls,
|
|
459
|
-
name: str,
|
|
460
|
-
value: Union[
|
|
461
|
-
str,
|
|
462
|
-
bytes,
|
|
463
|
-
Path,
|
|
464
|
-
typing.AsyncGenerator[bytes, str],
|
|
465
|
-
typing.Generator[bytes, str],
|
|
466
|
-
],
|
|
467
|
-
filename: Optional[str] = None,
|
|
468
|
-
mime: Optional[str] = None,
|
|
469
|
-
) -> Part:
|
|
470
|
-
r"""
|
|
471
|
-
Creates a new part.
|
|
472
|
-
|
|
473
|
-
# Arguments
|
|
474
|
-
- `name` - The name of the part.
|
|
475
|
-
- `value` - The value of the part, either text, bytes, a file path, or a async or sync stream.
|
|
476
|
-
- `filename` - The filename of the part.
|
|
477
|
-
- `mime` - The MIME type of the part.
|
|
478
|
-
"""
|
|
479
|
-
|
|
480
|
-
class Response:
|
|
481
|
-
r"""
|
|
482
|
-
A response from a request.
|
|
483
|
-
|
|
484
|
-
# Examples
|
|
485
|
-
|
|
486
|
-
```python
|
|
487
|
-
import asyncio
|
|
488
|
-
import rnet
|
|
489
|
-
|
|
490
|
-
async def main():
|
|
491
|
-
response = await rnet.get("https://www.rust-lang.org")
|
|
492
|
-
print("Status Code: ", response.status)
|
|
493
|
-
print("Version: ", response.version)
|
|
494
|
-
print("Response URL: ", response.url)
|
|
495
|
-
print("Headers: ", response.headers)
|
|
496
|
-
print("Content-Length: ", response.content_length)
|
|
497
|
-
print("Encoding: ", response.encoding)
|
|
498
|
-
print("Remote Address: ", response.remote_addr)
|
|
499
|
-
|
|
500
|
-
text_content = await response.text()
|
|
501
|
-
print("Text: ", text_content)
|
|
502
|
-
|
|
503
|
-
if __name__ == "__main__":
|
|
504
|
-
asyncio.run(main())
|
|
505
|
-
```
|
|
506
|
-
"""
|
|
507
|
-
|
|
508
|
-
url: str
|
|
509
|
-
r"""
|
|
510
|
-
Get the URL of the response.
|
|
511
|
-
"""
|
|
512
|
-
|
|
513
|
-
status: StatusCode
|
|
514
|
-
r"""
|
|
515
|
-
Get the status code of the response.
|
|
516
|
-
"""
|
|
517
|
-
|
|
518
|
-
version: Version
|
|
519
|
-
r"""
|
|
520
|
-
Get the HTTP version of the response.
|
|
521
|
-
"""
|
|
522
|
-
|
|
523
|
-
headers: HeaderMap
|
|
524
|
-
r"""
|
|
525
|
-
Get the headers of the response.
|
|
526
|
-
"""
|
|
527
|
-
|
|
528
|
-
cookies: List[Cookie]
|
|
529
|
-
r"""
|
|
530
|
-
Get the cookies of the response.
|
|
531
|
-
"""
|
|
532
|
-
|
|
533
|
-
content_length: int
|
|
534
|
-
r"""
|
|
535
|
-
Get the content length of the response.
|
|
536
|
-
"""
|
|
537
|
-
|
|
538
|
-
remote_addr: Optional[SocketAddr]
|
|
539
|
-
r"""
|
|
540
|
-
Get the remote address of the response.
|
|
541
|
-
"""
|
|
542
|
-
|
|
543
|
-
local_addr: Optional[SocketAddr]
|
|
544
|
-
r"""
|
|
545
|
-
Get the local address of the response.
|
|
546
|
-
"""
|
|
547
|
-
|
|
548
|
-
history: List[History]
|
|
549
|
-
r"""
|
|
550
|
-
Get the redirect history of the Response.
|
|
551
|
-
"""
|
|
552
|
-
|
|
553
|
-
peer_certificate: Optional[bytes]
|
|
554
|
-
r"""
|
|
555
|
-
Get the DER encoded leaf certificate of the response.
|
|
556
|
-
"""
|
|
557
|
-
|
|
558
|
-
async def text(self) -> str:
|
|
559
|
-
r"""
|
|
560
|
-
Get the text content of the response.
|
|
561
|
-
"""
|
|
562
|
-
|
|
563
|
-
async def text_with_charset(self, encoding: str) -> str:
|
|
564
|
-
r"""
|
|
565
|
-
Get the text content of the response with a specific charset.
|
|
566
|
-
|
|
567
|
-
# Arguments
|
|
568
|
-
|
|
569
|
-
* `encoding` - The default encoding to use if the charset is not specified.
|
|
570
|
-
"""
|
|
571
|
-
|
|
572
|
-
async def json(self) -> Any:
|
|
573
|
-
r"""
|
|
574
|
-
Get the JSON content of the response.
|
|
575
|
-
"""
|
|
576
|
-
|
|
577
|
-
async def bytes(self) -> bytes:
|
|
578
|
-
r"""
|
|
579
|
-
Get the bytes content of the response.
|
|
580
|
-
"""
|
|
581
|
-
|
|
582
|
-
def stream(self) -> Streamer:
|
|
583
|
-
r"""
|
|
584
|
-
Get the response into a `Stream` of `Bytes` from the body.
|
|
585
|
-
"""
|
|
586
|
-
|
|
587
|
-
async def close(self) -> None:
|
|
588
|
-
r"""
|
|
589
|
-
Close the response connection.
|
|
590
|
-
"""
|
|
591
|
-
|
|
592
|
-
async def __aenter__(self) -> Any: ...
|
|
593
|
-
async def __aexit__(
|
|
594
|
-
self, _exc_type: Any, _exc_value: Any, _traceback: Any
|
|
595
|
-
) -> Any: ...
|
|
596
|
-
|
|
597
|
-
class History:
|
|
598
|
-
"""
|
|
599
|
-
An entry in the redirect history.
|
|
600
|
-
"""
|
|
601
|
-
|
|
602
|
-
status: int
|
|
603
|
-
"""Get the status code of the redirect response."""
|
|
604
|
-
|
|
605
|
-
url: str
|
|
606
|
-
"""Get the URL of the redirect response."""
|
|
607
|
-
|
|
608
|
-
previous: str
|
|
609
|
-
"""Get the previous URL before the redirect response."""
|
|
610
|
-
|
|
611
|
-
headers: HeaderMap
|
|
612
|
-
"""Get the headers of the redirect response."""
|
|
613
|
-
|
|
614
|
-
def __str__(self) -> str: ...
|
|
615
|
-
|
|
616
|
-
class SocketAddr:
|
|
617
|
-
r"""
|
|
618
|
-
A IP socket address.
|
|
619
|
-
"""
|
|
620
|
-
|
|
621
|
-
def __str__(self) -> str: ...
|
|
622
|
-
def ip(self) -> Union[ipaddress.IPv4Address, ipaddress.IPv6Address]:
|
|
623
|
-
r"""
|
|
624
|
-
Returns the IP address of the socket address.
|
|
625
|
-
"""
|
|
626
|
-
|
|
627
|
-
def port(self) -> int:
|
|
628
|
-
r"""
|
|
629
|
-
Returns the port number of the socket address.
|
|
630
|
-
"""
|
|
631
|
-
|
|
632
|
-
class StatusCode:
|
|
633
|
-
r"""
|
|
634
|
-
HTTP status code.
|
|
635
|
-
"""
|
|
636
|
-
|
|
637
|
-
def __str__(self) -> str: ...
|
|
638
|
-
def as_int(self) -> int:
|
|
639
|
-
r"""
|
|
640
|
-
Return the status code as an integer.
|
|
641
|
-
"""
|
|
642
|
-
|
|
643
|
-
def is_informational(self) -> bool:
|
|
644
|
-
r"""
|
|
645
|
-
Check if status is within 100-199.
|
|
646
|
-
"""
|
|
647
|
-
|
|
648
|
-
def is_success(self) -> bool:
|
|
649
|
-
r"""
|
|
650
|
-
Check if status is within 200-299.
|
|
651
|
-
"""
|
|
652
|
-
|
|
653
|
-
def is_redirection(self) -> bool:
|
|
654
|
-
r"""
|
|
655
|
-
Check if status is within 300-399.
|
|
656
|
-
"""
|
|
657
|
-
|
|
658
|
-
def is_client_error(self) -> bool:
|
|
659
|
-
r"""
|
|
660
|
-
Check if status is within 400-499.
|
|
661
|
-
"""
|
|
662
|
-
|
|
663
|
-
def is_server_error(self) -> bool:
|
|
664
|
-
r"""
|
|
665
|
-
Check if status is within 500-599.
|
|
666
|
-
"""
|
|
667
|
-
|
|
668
|
-
class Streamer:
|
|
669
|
-
r"""
|
|
670
|
-
A byte stream response.
|
|
671
|
-
An asynchronous iterator yielding data chunks from the response stream.
|
|
672
|
-
Used to stream response content.
|
|
673
|
-
Implemented in the `stream` method of the `Response` class.
|
|
674
|
-
Can be used in an asynchronous for loop in Python.
|
|
675
|
-
|
|
676
|
-
# Examples
|
|
677
|
-
|
|
678
|
-
```python
|
|
679
|
-
import asyncio
|
|
680
|
-
import rnet
|
|
681
|
-
from rnet import Method, Emulation
|
|
682
|
-
|
|
683
|
-
async def main():
|
|
684
|
-
resp = await rnet.get("https://httpbin.org/stream/20")
|
|
685
|
-
async with resp.stream() as streamer:
|
|
686
|
-
async for chunk in streamer:
|
|
687
|
-
print("Chunk: ", chunk)
|
|
688
|
-
await asyncio.sleep(0.1)
|
|
1153
|
+
async def main():
|
|
1154
|
+
client = rnet.Client()
|
|
1155
|
+
response = await client.get("https://httpbin.org/anything")
|
|
1156
|
+
print(await response.text())
|
|
689
1157
|
|
|
690
|
-
if __name__ == "__main__":
|
|
691
1158
|
asyncio.run(main())
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
async def __aiter__(self) -> Streamer: ...
|
|
696
|
-
async def __anext__(self) -> Optional[bytes]: ...
|
|
697
|
-
async def __aenter__(self) -> Any: ...
|
|
698
|
-
async def __aexit__(
|
|
699
|
-
self, _exc_type: Any, _exc_value: Any, _traceback: Any
|
|
700
|
-
) -> Any: ...
|
|
701
|
-
def __iter__(self) -> Streamer: ...
|
|
702
|
-
def __next__(self) -> bytes: ...
|
|
703
|
-
def __enter__(self) -> Streamer: ...
|
|
704
|
-
def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> None: ...
|
|
1159
|
+
```
|
|
1160
|
+
"""
|
|
705
1161
|
|
|
706
1162
|
async def delete(
|
|
707
1163
|
url: str,
|
|
@@ -930,275 +1386,3 @@ async def websocket(
|
|
|
930
1386
|
asyncio.run(run())
|
|
931
1387
|
```
|
|
932
1388
|
"""
|
|
933
|
-
|
|
934
|
-
class Proxy:
|
|
935
|
-
r"""
|
|
936
|
-
A proxy server for a request.
|
|
937
|
-
Supports HTTP, HTTPS, SOCKS4, SOCKS4a, SOCKS5, and SOCKS5h protocols.
|
|
938
|
-
"""
|
|
939
|
-
|
|
940
|
-
@staticmethod
|
|
941
|
-
def http(url: str, **kwargs: Unpack[ProxyParams]) -> Proxy:
|
|
942
|
-
r"""
|
|
943
|
-
Creates a new HTTP proxy.
|
|
944
|
-
|
|
945
|
-
This method sets up a proxy server for HTTP requests.
|
|
946
|
-
|
|
947
|
-
# Arguments
|
|
948
|
-
|
|
949
|
-
* `url` - The URL of the proxy server.
|
|
950
|
-
* `username` - Optional username for proxy authentication.
|
|
951
|
-
* `password` - Optional password for proxy authentication.
|
|
952
|
-
* `custom_http_auth` - Optional custom HTTP proxy authentication header value.
|
|
953
|
-
* `custom_http_headers` - Optional custom HTTP proxy headers.
|
|
954
|
-
* `exclusion` - Optional List of domains to exclude from proxying.
|
|
955
|
-
|
|
956
|
-
# Examples
|
|
957
|
-
|
|
958
|
-
```python
|
|
959
|
-
import rnet
|
|
960
|
-
|
|
961
|
-
proxy = rnet.Proxy.http("http://proxy.example.com")
|
|
962
|
-
```
|
|
963
|
-
"""
|
|
964
|
-
|
|
965
|
-
@staticmethod
|
|
966
|
-
def https(url: str, **kwargs: Unpack[ProxyParams]) -> Proxy:
|
|
967
|
-
r"""
|
|
968
|
-
Creates a new HTTPS proxy.
|
|
969
|
-
|
|
970
|
-
This method sets up a proxy server for HTTPS requests.
|
|
971
|
-
|
|
972
|
-
# Arguments
|
|
973
|
-
|
|
974
|
-
* `url` - The URL of the proxy server.
|
|
975
|
-
* `username` - Optional username for proxy authentication.
|
|
976
|
-
* `password` - Optional password for proxy authentication.
|
|
977
|
-
* `custom_http_auth` - Optional custom HTTP proxy authentication header value.
|
|
978
|
-
* `custom_http_headers` - Optional custom HTTP proxy headers.
|
|
979
|
-
* `exclusion` - Optional List of domains to exclude from proxying.
|
|
980
|
-
|
|
981
|
-
# Examples
|
|
982
|
-
|
|
983
|
-
```python
|
|
984
|
-
import rnet
|
|
985
|
-
|
|
986
|
-
proxy = rnet.Proxy.https("https://proxy.example.com")
|
|
987
|
-
```
|
|
988
|
-
"""
|
|
989
|
-
|
|
990
|
-
@staticmethod
|
|
991
|
-
def all(url: str, **kwargs: Unpack[ProxyParams]) -> Proxy:
|
|
992
|
-
r"""
|
|
993
|
-
Creates a new proxy for all protocols.
|
|
994
|
-
|
|
995
|
-
This method sets up a proxy server for all types of requests (HTTP, HTTPS, etc.).
|
|
996
|
-
|
|
997
|
-
# Arguments
|
|
998
|
-
|
|
999
|
-
* `url` - The URL of the proxy server.
|
|
1000
|
-
* `username` - Optional username for proxy authentication.
|
|
1001
|
-
* `password` - Optional password for proxy authentication.
|
|
1002
|
-
* `custom_http_auth` - Optional custom HTTP proxy authentication header value.
|
|
1003
|
-
* `custom_http_headers` - Optional custom HTTP proxy headers.
|
|
1004
|
-
* `exclusion` - Optional List of domains to exclude from proxying.
|
|
1005
|
-
|
|
1006
|
-
# Examples
|
|
1007
|
-
|
|
1008
|
-
```python
|
|
1009
|
-
import rnet
|
|
1010
|
-
|
|
1011
|
-
proxy = rnet.Proxy.all("https://proxy.example.com")
|
|
1012
|
-
```
|
|
1013
|
-
"""
|
|
1014
|
-
|
|
1015
|
-
class Message:
|
|
1016
|
-
r"""
|
|
1017
|
-
A WebSocket message.
|
|
1018
|
-
"""
|
|
1019
|
-
|
|
1020
|
-
data: Optional[bytes]
|
|
1021
|
-
r"""
|
|
1022
|
-
Returns the data of the message as bytes.
|
|
1023
|
-
"""
|
|
1024
|
-
text: Optional[str]
|
|
1025
|
-
r"""
|
|
1026
|
-
Returns the text content of the message if it is a text message.
|
|
1027
|
-
"""
|
|
1028
|
-
binary: Optional[bytes]
|
|
1029
|
-
r"""
|
|
1030
|
-
Returns the binary data of the message if it is a binary message.
|
|
1031
|
-
"""
|
|
1032
|
-
ping: Optional[bytes]
|
|
1033
|
-
r"""
|
|
1034
|
-
Returns the ping data of the message if it is a ping message.
|
|
1035
|
-
"""
|
|
1036
|
-
pong: Optional[bytes]
|
|
1037
|
-
r"""
|
|
1038
|
-
Returns the pong data of the message if it is a pong message.
|
|
1039
|
-
"""
|
|
1040
|
-
close: Optional[Tuple[int, Optional[str]]]
|
|
1041
|
-
r"""
|
|
1042
|
-
Returns the close code and reason of the message if it is a close message.
|
|
1043
|
-
"""
|
|
1044
|
-
def __str__(self) -> str: ...
|
|
1045
|
-
@staticmethod
|
|
1046
|
-
def text_from_json(json: Dict[str, Any]) -> Message:
|
|
1047
|
-
r"""
|
|
1048
|
-
Creates a new text message from the JSON representation.
|
|
1049
|
-
|
|
1050
|
-
# Arguments
|
|
1051
|
-
* `json` - The JSON representation of the message.
|
|
1052
|
-
"""
|
|
1053
|
-
|
|
1054
|
-
@staticmethod
|
|
1055
|
-
def binary_from_json(json: Dict[str, Any]) -> Message:
|
|
1056
|
-
r"""
|
|
1057
|
-
Creates a new binary message from the JSON representation.
|
|
1058
|
-
|
|
1059
|
-
# Arguments
|
|
1060
|
-
* `json` - The JSON representation of the message.
|
|
1061
|
-
"""
|
|
1062
|
-
|
|
1063
|
-
@staticmethod
|
|
1064
|
-
def from_text(text: str) -> Message:
|
|
1065
|
-
r"""
|
|
1066
|
-
Creates a new text message.
|
|
1067
|
-
|
|
1068
|
-
# Arguments
|
|
1069
|
-
|
|
1070
|
-
* `text` - The text content of the message.
|
|
1071
|
-
"""
|
|
1072
|
-
|
|
1073
|
-
@staticmethod
|
|
1074
|
-
def from_binary(data: bytes) -> Message:
|
|
1075
|
-
r"""
|
|
1076
|
-
Creates a new binary message.
|
|
1077
|
-
|
|
1078
|
-
# Arguments
|
|
1079
|
-
|
|
1080
|
-
* `data` - The binary data of the message.
|
|
1081
|
-
"""
|
|
1082
|
-
|
|
1083
|
-
@staticmethod
|
|
1084
|
-
def from_ping(data: bytes) -> Message:
|
|
1085
|
-
r"""
|
|
1086
|
-
Creates a new ping message.
|
|
1087
|
-
|
|
1088
|
-
# Arguments
|
|
1089
|
-
|
|
1090
|
-
* `data` - The ping data of the message.
|
|
1091
|
-
"""
|
|
1092
|
-
|
|
1093
|
-
@staticmethod
|
|
1094
|
-
def from_pong(data: bytes) -> Message:
|
|
1095
|
-
r"""
|
|
1096
|
-
Creates a new pong message.
|
|
1097
|
-
|
|
1098
|
-
# Arguments
|
|
1099
|
-
|
|
1100
|
-
* `data` - The pong data of the message.
|
|
1101
|
-
"""
|
|
1102
|
-
|
|
1103
|
-
@staticmethod
|
|
1104
|
-
def from_close(code: int, reason: Optional[str] = None) -> Message:
|
|
1105
|
-
r"""
|
|
1106
|
-
Creates a new close message.
|
|
1107
|
-
|
|
1108
|
-
# Arguments
|
|
1109
|
-
|
|
1110
|
-
* `code` - The close code.
|
|
1111
|
-
* `reason` - An optional reason for closing.
|
|
1112
|
-
"""
|
|
1113
|
-
|
|
1114
|
-
def json(self) -> Dict[str, Any]:
|
|
1115
|
-
r"""
|
|
1116
|
-
Returns the JSON representation of the message.
|
|
1117
|
-
"""
|
|
1118
|
-
|
|
1119
|
-
class WebSocket:
|
|
1120
|
-
r"""
|
|
1121
|
-
A WebSocket response.
|
|
1122
|
-
"""
|
|
1123
|
-
|
|
1124
|
-
status: StatusCode
|
|
1125
|
-
r"""
|
|
1126
|
-
Get the status code of the response.
|
|
1127
|
-
"""
|
|
1128
|
-
version: Version
|
|
1129
|
-
r"""
|
|
1130
|
-
Get the HTTP version of the response.
|
|
1131
|
-
"""
|
|
1132
|
-
headers: HeaderMap
|
|
1133
|
-
r"""
|
|
1134
|
-
Get the headers of the response.
|
|
1135
|
-
"""
|
|
1136
|
-
cookies: List[Cookie]
|
|
1137
|
-
r"""
|
|
1138
|
-
Get the cookies of the response.
|
|
1139
|
-
"""
|
|
1140
|
-
remote_addr: Optional[SocketAddr]
|
|
1141
|
-
r"""
|
|
1142
|
-
Get the remote address of the response.
|
|
1143
|
-
"""
|
|
1144
|
-
protocol: Optional[str]
|
|
1145
|
-
r"""
|
|
1146
|
-
Get the WebSocket protocol.
|
|
1147
|
-
"""
|
|
1148
|
-
|
|
1149
|
-
def __aenter__(self) -> Any: ...
|
|
1150
|
-
def __aexit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> Any: ...
|
|
1151
|
-
async def recv(
|
|
1152
|
-
self, timeout: datetime.timedelta | None = None
|
|
1153
|
-
) -> Optional[Message]:
|
|
1154
|
-
r"""
|
|
1155
|
-
Receives a message from the WebSocket.
|
|
1156
|
-
"""
|
|
1157
|
-
|
|
1158
|
-
async def send(self, message: Message) -> None:
|
|
1159
|
-
r"""
|
|
1160
|
-
Sends a message to the WebSocket.
|
|
1161
|
-
|
|
1162
|
-
# Arguments
|
|
1163
|
-
|
|
1164
|
-
* `message` - The message to send.
|
|
1165
|
-
"""
|
|
1166
|
-
|
|
1167
|
-
async def close(
|
|
1168
|
-
self,
|
|
1169
|
-
code: Optional[int] = None,
|
|
1170
|
-
reason: Optional[str] = None,
|
|
1171
|
-
) -> None:
|
|
1172
|
-
r"""
|
|
1173
|
-
Closes the WebSocket connection.
|
|
1174
|
-
|
|
1175
|
-
# Arguments
|
|
1176
|
-
|
|
1177
|
-
* `code` - An optional close code.
|
|
1178
|
-
* `reason` - An optional reason for closing.
|
|
1179
|
-
"""
|
|
1180
|
-
|
|
1181
|
-
class Method(Enum):
|
|
1182
|
-
r"""
|
|
1183
|
-
An HTTP method.
|
|
1184
|
-
"""
|
|
1185
|
-
|
|
1186
|
-
GET = auto()
|
|
1187
|
-
HEAD = auto()
|
|
1188
|
-
POST = auto()
|
|
1189
|
-
PUT = auto()
|
|
1190
|
-
DELETE = auto()
|
|
1191
|
-
OPTIONS = auto()
|
|
1192
|
-
TRACE = auto()
|
|
1193
|
-
PATCH = auto()
|
|
1194
|
-
|
|
1195
|
-
class Version(Enum):
|
|
1196
|
-
r"""
|
|
1197
|
-
An HTTP version.
|
|
1198
|
-
"""
|
|
1199
|
-
|
|
1200
|
-
HTTP_09 = auto()
|
|
1201
|
-
HTTP_10 = auto()
|
|
1202
|
-
HTTP_11 = auto()
|
|
1203
|
-
HTTP_2 = auto()
|
|
1204
|
-
HTTP_3 = auto()
|