2016-06-17 5 views
1

난 그냥 리버스 프록시 매우 간단한 클라인 스크립트가 작동하지 않습니다 : 유일한 문제는 CSS가 작동하지 않는 것입니다클라인 스크립트 CSS는

from klein import run, route, Klein 
from twisted.web.proxy import ReverseProxyResource 

@route('/') 
def home(request, branch=True): 
    return ReverseProxyResource('www.example.com', 80, ''.encode('utf-8')) 

run("MY_IP", 80) 

을 할 때 웹 사이트는 상대적으로 사용하여 호출 경로 /css/example; 이 문제를 해결하는 방법을 모르겠습니다. 나는 어떤 제안이든 열려 있습니다.

저는 파이썬 3.3을 사용하고 있습니다.

+0

은 어쨌든 AJAX 호출을 통해로드 된 CSS입니다 (XMLHttpRequest?. if it i 이 경우 대부분 문제는 CORS와 관련이 있으며 원본 사이트의 허용 된 원본에 프록시 된 도메인을 추가하기 만하면됩니다. – Hani

답변

0

이 첫 번째 코드 블록은 내 첫 번째 패스이지만 작동하지 않습니다.

GET /a과 같이 작동하는 것으로 보였습니다.하지만 /<path>에는 /이 포함되어 있지 않기 때문입니다. 그래서 한 레벨보다 더 깊은 것은 무엇이든 프락시되지 않을 것입니다. @route으로 찾고

, 그것은 임의의 와일드 카드를 허용하는 werkzeug을하는 아래에 나타나지 않는다 사용하지만, 당신이 twisted 아래로 떨어 뜨린 경우

from klein import run 
from klein import route 
from twisted.web.proxy import ReverseProxyResource 

@route('/', defaults={'path': ''}) 
@route('/<path>') 
def home(request, path): 
    print "request: " + str(request) 
    print "path: " + path 
    return ReverseProxyResource('localhost', 8001, path.encode('utf-8')) 

run("localhost", 8000) 

을, 당신은 단순히이 작업을 수행 할 수 있습니다

# Copyright (c) Twisted Matrix Laboratories. 
# See LICENSE for details. 

""" 
This example demonstrates how to run a reverse proxy. 

Run this example with: 
    $ python reverse-proxy.py 

Then visit http://localhost:8000/ in your web browser. 
""" 

from twisted.internet import reactor 
from twisted.web import proxy, server 

site = server.Site(proxy.ReverseProxyResource('www.example.com', 80, '')) 
reactor.listenTCP(8000, site) 
reactor.run() 

각 요청을 포착, 기록, 수정 등을하려면 ReverseProxyResource을 서브 클래스 화하고 render()을 오버라이드 할 수 있습니다. 참고 :

from twisted.internet import reactor            
from twisted.web import proxy             
from twisted.web import server             
from twisted.python.compat import urlquote          

class MyReverseProxyResource(proxy.ReverseProxyResource):      

    def __init__(self, host='www.example.com', port=80, path='', reactor=reactor): 
     proxy.ReverseProxyResource.__init__(self, host, port, path, reactor)  

    def getChild(self, path, request):           
     # See https://twistedmatrix.com/trac/ticket/7806       
     return MyReverseProxyResource(           
      self.host, self.port, self.path + b'/' + urlquote(path, safe=b"").encode('utf-8'), 
      self.reactor)              

    def render(self, request):             
     print request               
     return proxy.ReverseProxyResource.render(self, request)     


p = MyReverseProxyResource()              
site = server.Site(p)               
reactor.listenTCP(8000, site)             
reactor.run() 

출력 : 당신은 또한 인해 buggetChild()를 오버라이드 (override) 할 필요가

<Request at 0x14e9f38 method=GET uri=/css/all.css?20130620 clientproto=HTTP/1.1> 
<Request at 0x15003b0 method=GET uri=/ clientproto=HTTP/1.1> 
+0

그래, 그게 합리적이긴하지만 이걸 CSS와 다른 링크)는 여전히 작동하지 않습니다. – Cristian

+0

어떤 종류의 오류가 있습니까? CSS에서 404? – rrauenza

+0

예, CSS의 404 오류 – Cristian

0

from klein import run, route, Klein 
from twisted.web.proxy import ReverseProxyResource 

@route('/',branch=True) 
def home(request): 
    return ReverseProxyResource('example.com', 80, ''.encode('utf-8')) 

run("MY_IP", 80) 

작동합니다 기본적으로 분기 주석이 아닌 가정을 @route하는 인수가 함수