克莱因脚本 CSS 不工作
klein script CSS isn't working
我有一个非常简单的 klein 脚本,它只是一个反向代理:
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.
基于您的第一段代码是我的第一遍,但它不起作用。
它似乎适用于 GET /a
,但那是因为 /<path>
不包括额外的 /
。所以任何比一层更深的东西都不会被代理。
查看 @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()
。注意:由于 bug:
,您还必须覆盖 getChild()
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>
这会起作用
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 注释的参数而不是 home 函数
我有一个非常简单的 klein 脚本,它只是一个反向代理:
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.
基于您的第一段代码是我的第一遍,但它不起作用。
它似乎适用于 GET /a
,但那是因为 /<path>
不包括额外的 /
。所以任何比一层更深的东西都不会被代理。
查看 @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()
。注意:由于 bug:
getChild()
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>
这会起作用
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 注释的参数而不是 home 函数