Downloading a file served by a tornado web server

971 views
Skip to first unread message

Eric

unread,
Jan 13, 2016, 4:20:35 PM1/13/16
to Tornado Web Server
This is how I have my tornado web server currently defined:

application = tornado.web.Application([
    tornado.web.url(r"/server", MainHandler),
    tornado.web.url(r"/(.*)", tornado.web.StaticFileHandler, { "path": scriptpath,  "default_filename": "index.html" }),
])

index.html is the start page of a web based gui. It will communicate with the backend server through http://<address>/server and the requests by the gui to the server are handled by the MainHandler function.

The directory structure looks like:

root_directory/
    server.py
    fileiwanttodownload.tar.gz
    index.html

I would like to be able to type into the browser:

http://<address>/data/fileiwanttodownload.tar.gz

and have the file delivered to me as a regular file download.

What I have tried to do is:

application = tornado.web.Application([
    tornado.web.url(r"/server", MainHandler),
    tornado.web.url(r"/data", tornado.web.StaticFileHandler, { "path": scriptpath } ),
    tornado.web.url(r"/(.*)", tornado.web.StaticFileHandler, { "path": scriptpath,  "default_filename": "index.html" }),
])

But this is not working for reasons that are probably obvious to those who know the answer.

The only clue I have is the following error message:

Uncaught exception GET /data (192.168.4.168)
HTTPServerRequest(protocol='http', host='192.168.4.195:8888', method='GET', uri='/data', version='HTTP/1.1', remote_ip='192.168.4.168', headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9', 'Host': '192.168.4.195:8888', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Connection': 'keep-alive', 'Accept-Language': 'en-us'})
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 1445, in _execute
    result = yield result
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1008, in run
    value = future.result()
  File "/usr/local/lib/python3.4/dist-packages/tornado/concurrent.py", line 232, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 267, in wrapper
    result = func(*args, **kwargs)
TypeError: get() missing 1 required positional argument: 'path'





Ben Darnell

unread,
Jan 13, 2016, 10:20:32 PM1/13/16
to Tornado Mailing List
On Wed, Jan 13, 2016 at 4:20 PM, Eric <eric...@gmail.com> wrote:
This is how I have my tornado web server currently defined:

application = tornado.web.Application([
    tornado.web.url(r"/server", MainHandler),
    tornado.web.url(r"/(.*)", tornado.web.StaticFileHandler, { "path": scriptpath,  "default_filename": "index.html" }),
])

index.html is the start page of a web based gui. It will communicate with the backend server through http://<address>/server and the requests by the gui to the server are handled by the MainHandler function.

The directory structure looks like:

root_directory/
    server.py
    fileiwanttodownload.tar.gz
    index.html

You probably want to put fileiwanttodownload.tar.gz in another directory by itself, so people can't ask for /data/server.py.
 

I would like to be able to type into the browser:

http://<address>/data/fileiwanttodownload.tar.gz

and have the file delivered to me as a regular file download.

What I have tried to do is:

application = tornado.web.Application([
    tornado.web.url(r"/server", MainHandler),
    tornado.web.url(r"/data", tornado.web.StaticFileHandler, { "path": scriptpath } ),
    tornado.web.url(r"/(.*)", tornado.web.StaticFileHandler, { "path": scriptpath,  "default_filename": "index.html" }),
])

The `/(.*)` is important: it's the regex capturing group that tells StaticFileHandler where the filename is in the URL. The `/data` mapping needs to be `/data/(.*)`.

-Ben
 

But this is not working for reasons that are probably obvious to those who know the answer.

The only clue I have is the following error message:

Uncaught exception GET /data (192.168.4.168)
HTTPServerRequest(protocol='http', host='192.168.4.195:8888', method='GET', uri='/data', version='HTTP/1.1', remote_ip='192.168.4.168', headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9', 'Host': '192.168.4.195:8888', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Connection': 'keep-alive', 'Accept-Language': 'en-us'})
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/web.py", line 1445, in _execute
    result = yield result
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 1008, in run
    value = future.result()
  File "/usr/local/lib/python3.4/dist-packages/tornado/concurrent.py", line 232, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/usr/local/lib/python3.4/dist-packages/tornado/gen.py", line 267, in wrapper
    result = func(*args, **kwargs)
TypeError: get() missing 1 required positional argument: 'path'





--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric

unread,
Jan 14, 2016, 9:32:05 AM1/14/16
to Tornado Web Server, b...@bendarnell.com
Bingo. The solution was to do r"/data/(.*)"
Reply all
Reply to author
Forward
0 new messages