kopia lustrzana https://github.com/cirospaciari/socketify.py
add form_data example
rodzic
81f5b1e4a1
commit
ed8b0eec3a
|
@ -0,0 +1,47 @@
|
|||
from socketify import App
|
||||
from streaming_form_data import StreamingFormDataParser
|
||||
from streaming_form_data.targets import ValueTarget, FileTarget
|
||||
app = App()
|
||||
router = app.router()
|
||||
|
||||
@router.post("/")
|
||||
async def upload(res, req):
|
||||
print(f"Posted to {req.get_url()}")
|
||||
parser = StreamingFormDataParser(headers=req.get_headers())
|
||||
name = ValueTarget()
|
||||
parser.register('name', name)
|
||||
file = FileTarget('/tmp/file')
|
||||
file2 = FileTarget('/tmp/file2')
|
||||
parser.register('file', file)
|
||||
parser.register('file2', file2)
|
||||
|
||||
|
||||
def on_data(res, chunk, is_end):
|
||||
parser.data_received(chunk)
|
||||
if is_end:
|
||||
res.cork(on_finish)
|
||||
|
||||
|
||||
def on_finish(res):
|
||||
print(name.value)
|
||||
|
||||
print(file.multipart_filename)
|
||||
print(file.multipart_content_type)
|
||||
|
||||
print(file2.multipart_filename)
|
||||
print(file2.multipart_content_type)
|
||||
|
||||
res.end("Thanks for the data!")
|
||||
|
||||
res.on_data(on_data)
|
||||
|
||||
|
||||
@router.any("*")
|
||||
def not_found(res, _):
|
||||
res.write_status(404).end("Not Found")
|
||||
|
||||
app.listen(
|
||||
3000,
|
||||
lambda config: print("Listening on port http://localhost:%d now\n" % config.port),
|
||||
)
|
||||
app.run()
|
|
@ -0,0 +1,21 @@
|
|||
from streaming_form_data import StreamingFormDataParser
|
||||
from socketify import Response
|
||||
def get_formdata(res: Response, parser: StreamingFormDataParser):
|
||||
_dataFuture = res.app.loop.create_future()
|
||||
|
||||
def is_aborted(res):
|
||||
res.aborted = True
|
||||
try:
|
||||
if not _dataFuture.done():
|
||||
_dataFuture.set_result(parser)
|
||||
except:
|
||||
pass
|
||||
|
||||
def get_chunks(res, chunk, is_end):
|
||||
parser.data_received(chunk)
|
||||
if is_end:
|
||||
_dataFuture.set_result(parser)
|
||||
|
||||
res.on_aborted(is_aborted)
|
||||
res.on_data(get_chunks)
|
||||
return _dataFuture
|
|
@ -78,6 +78,69 @@ async def upload_multiple(res, req):
|
|||
# We respond when we are done
|
||||
res.cork_end("Thanks for the data!")
|
||||
|
||||
def upload_formdata(res, req):
|
||||
# using streaming_form_data package for parsing
|
||||
from streaming_form_data import StreamingFormDataParser
|
||||
from streaming_form_data.targets import ValueTarget, FileTarget
|
||||
|
||||
print(f"Posted to {req.get_url()}")
|
||||
parser = StreamingFormDataParser(headers=req.get_headers())
|
||||
name = ValueTarget()
|
||||
parser.register('name', name)
|
||||
file = FileTarget('/tmp/file')
|
||||
file2 = FileTarget('/tmp/file2')
|
||||
parser.register('file', file)
|
||||
parser.register('file2', file2)
|
||||
|
||||
|
||||
def on_data(res, chunk, is_end):
|
||||
parser.data_received(chunk)
|
||||
if is_end:
|
||||
res.cork(on_finish)
|
||||
|
||||
|
||||
def on_finish(res):
|
||||
print(name.value)
|
||||
|
||||
print(file.multipart_filename)
|
||||
print(file.multipart_content_type)
|
||||
|
||||
print(file2.multipart_filename)
|
||||
print(file2.multipart_content_type)
|
||||
|
||||
res.end("Thanks for the data!")
|
||||
|
||||
res.on_data(on_data)
|
||||
|
||||
|
||||
async def upload_formhelper(res, req):
|
||||
# using streaming_form_data package for parsing + helper
|
||||
from streaming_form_data import StreamingFormDataParser
|
||||
from streaming_form_data.targets import ValueTarget, FileTarget
|
||||
from .helpers.form_data import get_formdata
|
||||
|
||||
|
||||
print(f"Posted to {req.get_url()}")
|
||||
parser = StreamingFormDataParser(headers=req.get_headers())
|
||||
name = ValueTarget()
|
||||
parser.register('name', name)
|
||||
file = FileTarget('/tmp/file')
|
||||
file2 = FileTarget('/tmp/file2')
|
||||
parser.register('file', file)
|
||||
parser.register('file2', file2)
|
||||
|
||||
await get_formdata(res, parser)
|
||||
|
||||
print(name.value)
|
||||
|
||||
print(file.multipart_filename)
|
||||
print(file.multipart_content_type)
|
||||
|
||||
print(file2.multipart_filename)
|
||||
print(file2.multipart_content_type)
|
||||
|
||||
res.cork_end("Thanks for the data!")
|
||||
|
||||
|
||||
app = App()
|
||||
app.post("/", upload)
|
||||
|
@ -86,6 +149,8 @@ app.post("/json", upload_json)
|
|||
app.post("/text", upload_text)
|
||||
app.post("/urlencoded", upload_urlencoded)
|
||||
app.post("/multiple", upload_multiple)
|
||||
app.post("/formdata", upload_formdata)
|
||||
app.post("/formdata2", upload_formdata)
|
||||
|
||||
app.any("/*", lambda res, _: res.write_status(404).end("Not Found"))
|
||||
app.listen(
|
||||
|
|
Ładowanie…
Reference in New Issue