I wrote a simple API that reads and writes files. A minimal version is below:
import flask
class Web:
def __init__(self):
self.app = flask.Flask('web')
self.app.add_url_rule('/read/<filename>', 'read', self.read)
self.app.run(host="0.0.0.0", port=6543)
def read(self, filename: str):
return open(filename).read()
Web()
Consider the following file (the one that will be read). It is saved as UTF-8
in Windows Notepad:
ascii
éàçèïê
When trying to download it via curl:
PS > curl http://localhost:6543/read/c5f89f95ef49492c82bdbcc133a5c567
ascii
éà çèïê
I tried to redirect it to a file, it is a mess as well:
It looks like that somewhere in the chain "read file with open()
" → "return it via flask" I made a mistake in encoding/decoding Unicode magic.
Try to use this construction:
open(filename, encoding="utf-8")