Skip to content

Instantly share code, notes, and snippets.

@pklaus
Forked from anjackson/gist:2888380
Last active January 12, 2019 15:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pklaus/5591338 to your computer and use it in GitHub Desktop.
Save pklaus/5591338 to your computer and use it in GitHub Desktop.
Mounting a proxy server to a /path of a Bottle app
import bottle
from hacks import ProxyServer
root = bottle.Bottle()
ps = ProxyServer()
root.mount(ps.wrapped_proxy_app, "/proxyapp")
@root.route('/hello/:name')
def index(name='World'):
return '<b>Hello %s!</b>' % name
#bottle.debug(True)
bottle.run(app=root, host='localhost', port=8080)
from wsgiproxy.app import WSGIProxyApp
# Remove "hop-by-hop" headers (as defined by RFC2613, Section 13)
# since they are not allowed by the WSGI standard.
FILTER_HEADERS = [
'Connection',
'Keep-Alive',
'Proxy-Authenticate',
'Proxy-Authorization',
'TE',
'Trailers',
'Transfer-Encoding',
'Upgrade',
]
def wrap_start_response(start_response):
def wrapped_start_response(status, headers_out):
# Remove "hop-by-hop" headers
headers_out = [(k,v) for (k,v) in headers_out
if k not in FILTER_HEADERS]
return start_response(status, headers_out)
return wrapped_start_response
class ProxyServer(object):
def __init__(self, url="http://localhost"):
self.URL = url
self.proxy_app = WSGIProxyApp(url)
def wrapped_proxy_app(self, environ, start_response):
start_response = wrap_start_response(start_response)
return self.proxy_app(environ, start_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment