kopia lustrzana https://github.com/cirospaciari/socketify.py
use BytesIO instead of chunks array
rodzic
8cce143d96
commit
3fe7e17ca0
|
@ -14,7 +14,7 @@ def upload(res, req):
|
||||||
```
|
```
|
||||||
|
|
||||||
### Getting it in an single call
|
### Getting it in an single call
|
||||||
We created an `res.get_data()` to get all data at once internally will create a list of bytes chunks for you.
|
We created an `res.get_data()` to get all data at once internally will create an BytesIO for you.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
async def upload_chunks(res, req):
|
async def upload_chunks(res, req):
|
||||||
|
@ -22,7 +22,7 @@ async def upload_chunks(res, req):
|
||||||
# await all the data, returns received chunks if fail (most likely fail is aborted requests)
|
# await all the data, returns received chunks if fail (most likely fail is aborted requests)
|
||||||
data = await res.get_data()
|
data = await res.get_data()
|
||||||
|
|
||||||
print(f"Got chunks {len(data)} of data!")
|
print(f"Got {len(data.getvalue())} bytes of data!")
|
||||||
|
|
||||||
# We respond when we are done
|
# We respond when we are done
|
||||||
res.cork_end("Thanks for the data!")
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
|
@ -21,7 +21,7 @@ async def upload_chunks(res, req):
|
||||||
# await all the data, returns received chunks if fail (most likely fail is aborted requests)
|
# await all the data, returns received chunks if fail (most likely fail is aborted requests)
|
||||||
data = await res.get_data()
|
data = await res.get_data()
|
||||||
|
|
||||||
print(f"Got chunks {len(data)} of data!")
|
print(f"Got {len(data.getvalue())} bytes of data!")
|
||||||
|
|
||||||
# We respond when we are done
|
# We respond when we are done
|
||||||
res.cork_end("Thanks for the data!")
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
|
@ -3,6 +3,7 @@ from datetime import datetime
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from http import cookies
|
from http import cookies
|
||||||
import inspect
|
import inspect
|
||||||
|
from io import BytesIO
|
||||||
import json
|
import json
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
|
@ -1601,7 +1602,7 @@ class AppResponse:
|
||||||
try:
|
try:
|
||||||
# decode and unquote all
|
# decode and unquote all
|
||||||
result = {}
|
result = {}
|
||||||
parsed = parse_qs(b"".join(data), encoding=encoding)
|
parsed = parse_qs(data.getvalue(), encoding=encoding)
|
||||||
has_value = False
|
has_value = False
|
||||||
for key in parsed:
|
for key in parsed:
|
||||||
has_value = True
|
has_value = True
|
||||||
|
@ -1620,14 +1621,14 @@ class AppResponse:
|
||||||
async def get_text(self, encoding="utf-8"):
|
async def get_text(self, encoding="utf-8"):
|
||||||
data = await self.get_data()
|
data = await self.get_data()
|
||||||
try:
|
try:
|
||||||
return b"".join(data).decode(encoding)
|
return data.getvalue().decode(encoding)
|
||||||
except Exception:
|
except Exception:
|
||||||
return None # invalid encoding
|
return None # invalid encoding
|
||||||
|
|
||||||
async def get_json(self):
|
async def get_json(self):
|
||||||
data = await self.get_data()
|
data = await self.get_data()
|
||||||
try:
|
try:
|
||||||
return json.loads(b"".join(data).decode("utf-8"))
|
return json.loads(data.getvalue().decode("utf-8"))
|
||||||
except Exception:
|
except Exception:
|
||||||
return None # invalid json
|
return None # invalid json
|
||||||
|
|
||||||
|
@ -1674,7 +1675,7 @@ class AppResponse:
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
self._dataFuture = self.loop.create_future()
|
self._dataFuture = self.loop.create_future()
|
||||||
self._data = []
|
self._data = BytesIO()
|
||||||
|
|
||||||
def is_aborted(self):
|
def is_aborted(self):
|
||||||
self.aborted = True
|
self.aborted = True
|
||||||
|
@ -1685,7 +1686,7 @@ class AppResponse:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_chunks(self, chunk, is_end):
|
def get_chunks(self, chunk, is_end):
|
||||||
self._data.append(chunk)
|
self._data.write(chunk)
|
||||||
if is_end:
|
if is_end:
|
||||||
self._dataFuture.set_result(self._data)
|
self._dataFuture.set_result(self._data)
|
||||||
self._data = None
|
self._data = None
|
||||||
|
|
Ładowanie…
Reference in New Issue