如何在 python tornado 上加载 html 图像文件

how to load html image files on the python tornado

我想制作一个网页。但我无法在 html 中加载图像文件。我不知道为什么会出现这种情况。我附上 HTML 代码,python tornado 代码。 你能帮帮我吗?

HTML 文件

<!DOCTYPE html>
<html>
<body>
<p>The GIF standard allows moving images.</p>
<img src="programming.gif" alt="Computer man" style="width:148px;hei    ght:148px;">
</body>
</html>

PYTHON-龙卷风文件

import tornado.web
import tornado.ioloop
import tornado.httpserver

class Handler(tornado.web.RequestHandler):
  def get(self):
    self.render("html_image_05.html")

application = tornado.web.Application([
(r"/",Handler)
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(80)
tornado.ioloop.IOLoop.instance().start()

这是我执行此代码时的消息代码。

WARNING:tornado.access:404 GET /eh5v.files/html5video/test.jpg (127.0.0.1) 0.56ms
WARNING:tornado.access:404 GET /eh5v.files/html5video/html5ext.js (127.0.0.1) 0.41ms
WARNING:tornado.access:404 GET /eh5v.files/html5video/test.m4v (127.0.0.1) 0.48ms
WARNING:tornado.access:404 GET /eh5v.files/html5video/test.jpg (127.0.0.1) 0.34ms
WARNING:tornado.access:404 GET /eh5v.files/html5video/test.webm (127.0.0.1) 0.36ms

问题解决了。 就是这样。

Python 文件代码(固定)

import tornado.web
import tornado.ioloop
import tornado.httpserver

class Handler(tornado.web.RequestHandler):
    def get(self):
        self.render("html_image_05.html")

application = tornado.web.Application([
(r"/",Handler),
(r"/(programming.gif)", tornado.web.StaticFileHandler, {'path':'./'}) <--Add!
])

http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(80)
tornado.ioloop.IOLoop.instance().start()

这是关于将图像与 html 和 python 文件连接的解决方案。

我在 Tornado 官方文档中找到了这个。

A StaticFileHandler is configured automatically if you pass the static_path keyword argument to Application. This handler can be customized with the static_url_prefix, static_handler_class, and static_handler_args settings.

当前版本是 6.0.4 版

这意味着你应该能够做这样的事情:

import tornado.web
import tornado.ioloop
import tornado.httpserver

class Handler(tornado.web.RequestHandler):
  def get(self):
    self.render("html_image_05.html")

application = tornado.web.Application([
(r"/",Handler)
],

static_path = os.path.join(os.path.dirname(__file__),"static"))

http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(80)
tornado.ioloop.IOLoop.instance().start()

将图像放入 static 文件夹后,您可以通过以下方式稍微更改 .html 文件:

<img src="static/programming.gif" alt="Computer man" style="width:148px; hei  ght:148px;">