【问题标题】:Cherrypy and JS, can't find imageCherrypy 和 JS,找不到图像
【发布时间】:2017-09-21 11:33:44
【问题描述】:

我有一个简单的 Cherrypy 脚本,现在只提供一个页面。我希望页面能够动态显示图像。为此,我编写了一个简单的 JS 脚本。但是,当我尝试运行该页面时,它找不到图像。代码从~/image_player/test_app.py 运行,图像在~/image_player/app/public。静态路径见python代码:

import cherrypy
import os
import sys


class image_player(object):
    @cherrypy.expose
    def index(self):
        return open('app/index.html')


if __name__ == '__main__':
    if len(sys.argv) == 2:
        port = int(sys.argv[1])
    else:
        port = 3030
    host = '0.0.0.0'
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/query': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type', 'text/plain')],
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'app/public'
        },
        '/js': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'app/js'
        }
    }

    webapp = image_player()
    # Configure server and port
    cherrypy.config.update({'server.socket_host': host,
                            'server.socket_port': port})
    cherrypy.quickstart(webapp, '/', conf)

这是包含 js 的 index.html

<!DOCTYPE html>
<html>
  <head>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
  hello
  <div id="imageDiv"></div>
  <script>
    var par = document.getElementById('imageDiv');
    var img = document.createElement('img');
    img.src = '/LPROFILE.jpg';
    par.appendChild(img);
  </script>
  </body>
</html>

我得到的错误是 GET http://hostname/LPROFILE.jpg 404 (Not Found) 我显然在这里遗漏了一些简单的东西,但我不确定是什么。

【问题讨论】:

    标签: javascript python cherrypy


    【解决方案1】:

    它适用于我(win x64):

    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/css': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './css'},
        '/img': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './img'},
        '/js': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './js'},
        'global': {
            'environment': 'production',
            'log.screen': True,
            'server.socket_host': '127.0.0.1',
            'server.socket_port': 8080,
            'engine.autoreload_on': True,
    
        }}
    

    例如: 网址(../img/backgr.jpg)

    【讨论】:

      【解决方案2】:

      根据您显示的配置,静态文件在/static 路径下提供,这意味着app/public 下的所有文件(相对于您启动服务器的初始目录)都可以从http://hostname/static/,对于LPROFILE.jpg,应该在:http://hostname/static/LPROFILE.jpg中可用

      【讨论】:

      • 这行得通,谢谢! Cherrypy newb 在这里,非常感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多