Skip to content

Instantly share code, notes, and snippets.

@lwzm
Last active July 27, 2019 09:30
Show Gist options
  • Save lwzm/7106a792b2df365177455b0819e8da06 to your computer and use it in GitHub Desktop.
Save lwzm/7106a792b2df365177455b0819e8da06 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import gzip
import io
import shutil
import falcon
'''
with open(fn, 'rb') as z, open('out', "w") as out:
with gzip.open(z, 'rt') as f:
for i in f:
#print(i)
#time.sleep(0.1)
out.write(i)
# or
with open(fn, 'rb') as z, open('out', "w") as out:
with gzip.open(z) as f:
# need not
# with io.TextIOWrapper(f) as ...:
# ....
for i in io.TextIOWrapper(f):
#print(i)
#time.sleep(0.1)
out.write(i)
'''
def iter_file(filename):
with open(filename, 'rb') as f:
while True:
bs = f.read(4096)
if not bs:
break
yield bs
class Upload:
def on_get(self, req, resp, fn):
#resp.stream = open(fn) # incompatible with bjoern
resp.stream = iter_file(fn)
def on_post(self, req, resp, fn):
if req.get_header('Content-Encoding') == 'gzip':
src = gzip.open(req.stream)
else:
src = req.stream
shutil.copyfileobj(src, open(fn, 'wb'), 4096)
api = application = falcon.API()
api.add_route('/{fn}', Upload())
if __name__ == '__main__':
import bjoern
bjoern.run(api, "", 8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment