下载龙卷风Web服务器提供的文件(Downloading a file served by a tornado web server)

编程入门 行业动态 更新时间:2024-10-18 14:19:19
下载龙卷风Web服务器提供的文件(Downloading a file served by a tornado web server)

这就是我目前定义龙卷风Web服务器的方式:

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是基于web的gui的起始页面。 它将通过http:/// server与后端服务器通信,gui对服务器的请求由MainHandler函数处理。

目录结构如下:

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

我希望能够输入浏览器:

HTTP:///data/fileiwanttodownload.tar.gz

并将文件作为常规文件下载发送给我。

我试图做的是:

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" }), ])

但这并不适用于那些知道答案的人可能会明白的原因。

我唯一的线索是以下错误消息:

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'

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:///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:///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'

最满意答案

你没有显示的scriptpath ,可能是错误的。 在path您应该为文件提供根目录,在URI匹配器中仅捕获文件左右。 简单的例子:

import tornado.ioloop import tornado.web import os class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") def make_app(): script_path = os.path.dirname(__file__) return tornado.web.Application([ (r"/", MainHandler), (r"/data/(.*)", tornado.web.StaticFileHandler, {"path": script_path}), # ^ we capture only this part ]) if __name__ == "__main__": app = make_app() app.listen(8888) tornado.ioloop.IOLoop.current().start()

您可以运行它,但建议将静态/数据文件存储在单独的目录中,因为可以从应用程序根目录下载所有内容,包括python one。

所以把你的可下载文件放在data子目录中然后

script_path = os.path.join(os.path.dirname(__file__), 'data')

有关StaticFileHandler的更多信息。

编辑

您得到的错误是因为在您的代码/data路由中有StaticFileHandler,但没有从请求的路径捕获() 。

scriptpath you haven't shown, is probably wrong. In path you should provide root dir to the files, in URI matcher capture only the file or so. Simple example:

import tornado.ioloop import tornado.web import os class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") def make_app(): script_path = os.path.dirname(__file__) return tornado.web.Application([ (r"/", MainHandler), (r"/data/(.*)", tornado.web.StaticFileHandler, {"path": script_path}), # ^ we capture only this part ]) if __name__ == "__main__": app = make_app() app.listen(8888) tornado.ioloop.IOLoop.current().start()

As you can run it works, but it is recommended to store static/data files in separate directory, because it is possible to download everything from the app root dir, including python one.

So put your downloadable file e.g. in data subdirectory and then

script_path = os.path.join(os.path.dirname(__file__), 'data')

More info about StaticFileHandler.

edit

The error you are getting is because in your code /data route has StaticFileHandler, but nothing is captured () from requested path.

更多推荐

本文发布于:2023-04-29 01:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:龙卷风   服务器   文件   Web   Downloading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!