2012-03-02 2 views
4

나는 우분투를 사용하고 있으며 "webchat"라는 디렉토리를 가지고 있습니다.이 디렉토리 아래에는 webchat.py, webchat.css, webchat.html, webchat.js의 4 개 파일이 있습니다. Tornado의 파일에 HTTP GET 요청을 처리하는 방법은 무엇입니까?

토네이도를 사용하여 HTTP 서버를 생성

, 난 내 파이썬 코드에 루트 ("/")지도 : 'webchat.py'을 다음과 같이 :

import os,sys 
import tornado.ioloop 
import tornado.web 
import tornado.httpserver 

#http server for webchat 
class webchat(tornado.web.RequestHandler): 
    def get(self): 
    self.write("Hello, chatter! [GET]") 
    def post(self): 
    self.write("Hello, chatter! [POST]") 

#create http server 
Handlers  = [(r"/",webchat)] 
App_Settings = {"debug":True} 
HTTP_Server = tornado.web.Application(Handlers,**App_Settings) 

#run http server 
HTTP_Server.listen(9999) 
tornado.ioloop.IOLoop.instance().start() 

http://localhost:9999에 액세스하면 '웹 채팅에 저를 이끌 '핸들러 (클래스 웹캠). 그러나, 나는 webchat.css, webchat.html, webchat.js 같은 'webchat.py'와 같은 디렉토리에있는 다른 파일에 액세스하고 싶습니다.

이 URL은 404 : http://localhost:9999/webchat.html입니다. 이 문제에 대한 가능한 해결책은 무엇입니까? 전용 파일 이름과 상대 경로와 간단한 파일 요청에 대한

답변

12

토네이도가 기본 정적 파일 처리기를 가지고 있지만, 그것은/정적에 URL을 매핑/당신이에서 정적 파일에 액세스해야하는 경우가 확인 될 것입니다/정적/webchat. CSS?

괜찮 으면이 방법으로 정적 파일을 처리하는 것이 좋습니다.

정적 파일을 루트 경로로 사용하려면 web.StaticFileHandler를 한눈에 살펴보십시오.

이 경우 당신은 여기를 놓친 예를 파이썬에서

(r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}), 

BTW, File_NameHandlers이 고려되지 않은 좋은 변수 이름입니다.

5

솔루션 :

(1) 핸들러 URL 패턴에게 살쾡이 보내기

Handlers = [(r"/(.*)",webchat)] 

(2) (가 제시 한 매개 변수를 전달합니다. *) '포스트'방법 'GET'와에 :

def get(self,File_Name): 
    File = open(File_Name,"r") 
    self.write(File.read()) 
    File.close() 

def post(self,File_Name): 
    File = open(File_Name,"r") 
    self.write(File.read()) 
    File.close()  
+1

보안 문제는 어떻게됩니까? – JulienFr

+0

@jondinham 정말 저를 위해 일해 주셔서 감사합니다 :) –

관련 문제