fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
from socketify import App
|
|
|
|
|
|
|
|
###
|
2022-11-28 05:44:38 +00:00
|
|
|
# We always recommend check res.aborted in async operations
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
###
|
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
def upload(res, req):
|
|
|
|
print(f"Posted to {req.get_url()}")
|
|
|
|
|
|
|
|
def on_data(res, chunk, is_end):
|
|
|
|
print(f"Got chunk of data with length {len(chunk)}, is_end: {is_end}")
|
2022-11-16 19:28:46 +00:00
|
|
|
if is_end:
|
2022-10-27 12:51:15 +00:00
|
|
|
res.cork_end("Thanks for the data!")
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
|
|
|
|
res.on_data(on_data)
|
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
async def upload_chunks(res, req):
|
|
|
|
print(f"Posted to {req.get_url()}")
|
2022-11-16 19:28:46 +00:00
|
|
|
# await all the data, returns received chunks if fail (most likely fail is aborted requests)
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
data = await res.get_data()
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-11-30 11:56:27 +00:00
|
|
|
print(f"Got {len(data.getvalue())} bytes of data!")
|
2022-11-17 21:36:01 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
# We respond when we are done
|
2022-10-27 12:51:15 +00:00
|
|
|
res.cork_end("Thanks for the data!")
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
async def upload_json(res, req):
|
|
|
|
print(f"Posted to {req.get_url()}")
|
2022-11-16 19:28:46 +00:00
|
|
|
# await all the data and parses as json, returns None if fail
|
2022-11-17 21:36:01 +00:00
|
|
|
info = await res.get_json()
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-11-17 21:36:01 +00:00
|
|
|
print(info)
|
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
# We respond when we are done
|
2022-10-27 12:51:15 +00:00
|
|
|
res.cork_end("Thanks for the data!")
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
async def upload_text(res, req):
|
|
|
|
print(f"Posted to {req.get_url()}")
|
2022-11-16 19:28:46 +00:00
|
|
|
# await all the data and decode as text, returns None if fail
|
|
|
|
text = await res.get_text() # first parameter is the encoding (default utf-8)
|
|
|
|
|
2022-11-17 21:36:01 +00:00
|
|
|
print(f"Your text is {text}")
|
2022-11-16 19:28:46 +00:00
|
|
|
|
|
|
|
# We respond when we are done
|
2022-10-27 12:51:15 +00:00
|
|
|
res.cork_end("Thanks for the data!")
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-06-03 23:01:56 +00:00
|
|
|
async def upload_urlencoded(res, req):
|
|
|
|
print(f"Posted to {req.get_url()}")
|
2022-11-16 19:28:46 +00:00
|
|
|
# await all the data and decode as application/x-www-form-urlencoded, returns None if fails
|
|
|
|
form = (
|
|
|
|
await res.get_form_urlencoded()
|
|
|
|
) # first parameter is the encoding (default utf-8)
|
|
|
|
|
2022-11-17 21:36:01 +00:00
|
|
|
print(f"Your form is {form}")
|
2022-11-16 19:28:46 +00:00
|
|
|
|
|
|
|
# We respond when we are done
|
2022-10-27 12:51:15 +00:00
|
|
|
res.cork_end("Thanks for the data!")
|
2022-06-03 23:01:56 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-06-03 23:01:56 +00:00
|
|
|
async def upload_multiple(res, req):
|
|
|
|
print(f"Posted to {req.get_url()}")
|
|
|
|
content_type = req.get_header("content-type")
|
2022-11-16 19:28:46 +00:00
|
|
|
# we can check the Content-Type to accept multiple formats
|
2022-06-03 23:01:56 +00:00
|
|
|
if content_type == "application/json":
|
|
|
|
data = await res.get_json()
|
|
|
|
elif content_type == "application/x-www-form-urlencoded":
|
|
|
|
data = await res.get_form_urlencoded()
|
|
|
|
else:
|
|
|
|
data = await res.get_text()
|
2022-11-16 19:28:46 +00:00
|
|
|
|
2022-11-17 21:36:01 +00:00
|
|
|
print(f"Your data is {data}")
|
2022-11-16 19:28:46 +00:00
|
|
|
|
|
|
|
# We respond when we are done
|
2022-10-27 12:51:15 +00:00
|
|
|
res.cork_end("Thanks for the data!")
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
|
2023-03-09 10:15:28 +00:00
|
|
|
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
|
2023-03-09 10:17:18 +00:00
|
|
|
from helpers.form_data import get_formdata
|
2023-03-09 10:15:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
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!")
|
|
|
|
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
|
|
|
|
app = App()
|
|
|
|
app.post("/", upload)
|
|
|
|
app.post("/chunks", upload_chunks)
|
|
|
|
app.post("/json", upload_json)
|
|
|
|
app.post("/text", upload_text)
|
2022-06-03 23:01:56 +00:00
|
|
|
app.post("/urlencoded", upload_urlencoded)
|
|
|
|
app.post("/multiple", upload_multiple)
|
2023-03-09 10:15:28 +00:00
|
|
|
app.post("/formdata", upload_formdata)
|
2023-03-09 10:17:18 +00:00
|
|
|
app.post("/formdata2", upload_formhelper)
|
fixed UVLoop, and added better strategy to Loop, added post support, get_data(), get_json(), get_text(), get_cookie(), set_cookie(), pending x-www-form-urlencoded and form-data (maybe only with another package), added more options for keys in response, added fallback to None if decode fails in request, added on_writable and on_data events, pending try_end and stream examples, added upload and post examples
2022-06-03 21:54:00 +00:00
|
|
|
|
2022-11-16 19:28:46 +00:00
|
|
|
app.any("/*", lambda 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()
|