2017-04-24 4 views
0

간단한 Cherrypy 스크립트가 있습니다. 이제는 페이지 만 제공합니다. 페이지가 이미지를 동적으로 표시 할 수 있기를 원합니다. 이를 위해 간단한 JS 스크립트를 작성했습니다. 그러나 페이지를 실행하려고하면 이미지를 찾을 수 없습니다. 코드는 ~/image_player/test_app.py에서 실행되며 이미지는 ~/image_player/app/public입니다. 정적 경로의 파이썬 코드를 참조하십시오 여기Cherrypy 및 JS, 이미지를 찾을 수 없습니다.

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) 

및 년대 index.html 포함 된 JS는 :

<!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)하지만 난 아니에요 물론.

답변

2

주어진 구성에서 정적 파일은 /static 경로로 제공됩니다. 즉 app/public (사용자가 서버를 시작한 초기 디렉토리와 비교) 아래의 모든 파일은 http://hostname/static/에서 액세스 할 수 있습니다. LPROFILE.jpg의 경우는 다음에서 사용할 수 있습니다. http://hostname/static/LPROFILE.jpg

+0

감사합니다. 여기 Cherrypy newb 그래서 도움을 주셔서 감사합니다. –