Created
February 16, 2017 00:56
-
-
Save dexterous/e2e3a3cd10f31304651a9fa788972fee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<form method="GET" action="/file" target="_blank"> | |
<input name="filename" type="text" /> | |
<input name="submit" type="submit" /> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from os.path import dirname, join | |
from urllib import quote | |
from re import sub | |
from tornado.httpserver import HTTPServer | |
from tornado.ioloop import IOLoop | |
from tornado.web import Application, RequestHandler, StaticFileHandler | |
def header_quote(value): | |
result = sub(r'\t', r' ', value) | |
result = sub(r'["\\\t]', r'\\\g<0>', result) | |
return result | |
class FileHandler(RequestHandler): | |
def get(self): | |
# filename = quote(self.get_query_argument('filename')) | |
filename = self.get_query_argument('filename') | |
filename = header_quote(filename) | |
header_value = 'attachment; filename="{}"'.format(filename) | |
print(header_value) | |
self.set_header('Content-Disposition', header_value) | |
# self.set_header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') | |
self.set_header('Content-Type', 'text/plain') | |
self.write('This is the file: {}'.format(self.get_query_argument('filename'))) | |
self.finish() | |
SETTINGS = dict( | |
static_path=join(dirname(__file__), 'static'), | |
debug=True | |
) | |
HANDLERS = [ | |
(r'/(download\.html)', StaticFileHandler, {'path': SETTINGS['static_path']}), | |
(r'/file', FileHandler) | |
] | |
def app(): | |
return Application( | |
HANDLERS, | |
**SETTINGS | |
) | |
def main(): | |
HTTPServer(app()).listen(8000) | |
print('Listening on port 8000') | |
IOLoop.current().start() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment