From ed8b0eec3a818d1ce66d0159d6bd855e68c78a01 Mon Sep 17 00:00:00 2001 From: Ciro Date: Thu, 9 Mar 2023 07:15:28 -0300 Subject: [PATCH] add form_data example --- examples/form_data.py | 47 +++++++++++++++++++++++++ examples/helpers/form_data.py | 21 +++++++++++ examples/upload_or_post.py | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 examples/form_data.py create mode 100644 examples/helpers/form_data.py diff --git a/examples/form_data.py b/examples/form_data.py new file mode 100644 index 0000000..d37a742 --- /dev/null +++ b/examples/form_data.py @@ -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() diff --git a/examples/helpers/form_data.py b/examples/helpers/form_data.py new file mode 100644 index 0000000..e7fde0d --- /dev/null +++ b/examples/helpers/form_data.py @@ -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 \ No newline at end of file diff --git a/examples/upload_or_post.py b/examples/upload_or_post.py index 8b391b2..2064865 100644 --- a/examples/upload_or_post.py +++ b/examples/upload_or_post.py @@ -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(