2016-06-17 10 views
1

が動作していない:クラインスクリプト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が動作しないですパス/css/example;私はこれをどのように修正するか分からない。私はどんな提案にも開放的です。

私はPython-3.3を使用しています。

+0

は、とにかくAJAX呼び出しによってロードされるCSSです(XMLHttpRequest?if it iあなたの問題はほとんどがCORSに関連しており、元のサイトの許可元にプロキシされたドメインを追加するだけです。 – Hani

答えて

0

この最初のコードブロックは私の最初のパスでしたが、動作しません。

GET /aのように動作するようですが、/<path>には/が含まれていないためです。したがって、1つのレベルより深いものはプロキシされません。

@routeに見ると、それは任意のワイルドカードを許可するように表示されません。その下にwerkzeugを使用しています。

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) 

あなたがダウンtwistedにドロップすると、しかし、あなたは、単にこれを行うことができます。

# 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()を上書きすることができます。注:あなたも原因buggetChild()を上書きする必要があります。

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() 

出力:

<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

どのようなエラーが表示されますか? 404上の404? – rrauenza

+0

正確には、404エラーCSSの – 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の引数ではない家であります関数

関連する問題