Code Documentation

wsgiproxy.exactproxy - Most basic HTTP proxying

This module is what all the other proxying is based on. This takes the WSGI request and sends it to another host. There is no configuration, as it uses the request itself to determine where to send the request.

Note: you can have different values for environ['SERVER_NAME'] and environ['HTTP_HOST']. The request will be sent to SERVER_NAME, but with the HTTP_HOST header. (Apache can do this as well with ProxyPreserveHost.)

wsgiproxy.exactproxy.proxy_exact_request(environ, start_response)

HTTP proxying WSGI application that proxies the exact request given in the environment. All controls are passed through the environment.

This connects to the server given in SERVER_NAME:SERVER_PORT, and sends the Host header in HTTP_HOST – they do not have to match.

Does not add X-Forwarded-For or other standard headers

Used with WebOb you can use this as a HTTP client library, like:

>>> from webob import Request
>>> from wsgiproxy.exactproxy import proxy_exact_request
>>> req = Request.blank('http://python.org')
>>> resp = req.get_response(proxy_exact_request)

You can use this to send POST request, file uploads, etc. WebOb request objects are fully writable.

A smaller feature that can be used with paste.httpserver is the ability to run the Paste server as a true HTTP proxy (like you would configure in your browser). The Paste server sets some keys in the environ (e.g., paste.httpserver.proxy.host) to indicate details of the original request.

If you use:

>>> app = filter_paste_httpserver_proxy(proxy_exact_request)

it will send the request where it was originally destined.

wsgiproxy.app - Send request to another host

This module sends incoming requests to another host. It will rewrite the Host header and other information as the request comes in (unlike exactproxy.proxy_exact_request()).

class wsgiproxy.app.WSGIProxyApp(href, secret_file=None, string_keys=None, unicode_keys=None, json_keys=None, pickle_keys=None)

Acts as a WSGI application that sends all its requests to another host

This rewrites incoming requests and sends them to another host (identified by href). By default it will set a few standard headers:

``X-Forwarded-Server``: The host name where the request originated

X-Forwarded-Scheme: The original scheme (e.g., http)

X-Forwarded-For: The address of the original client (REMOTE_ADDR)

X-Forwarded-Script-Name: The value of SCRIPT_NAME

X-Traversal-Path: The portion of the destination (href) path that is being forwarded to; this part of the path was not in the original request

X-Traversal-Query-String: Any portion of the query string that was not in the original request

wsgiproxy.middleware - Fix up incoming requests

This middleware looks at the headers that wsgiproxy.app.WSGIProxyApp adds (many but not all of which are used by other HTTP forwarding servers) and fixes up the request environment to put those headers back in place.

class wsgiproxy.middleware.WSGIProxyMiddleware(application, secret_file=None, trust_ips=None, prefix=None, pop_prefix=None, scheme=None, host=None, domain=None, port=None)

Fixes up the environment given special headers set by WSGIProxy, or by configuration.

Accepts the configuration:

secret_file:

A location where a secret is kept, used to sign the request when its coming from wsgiproxy.app

trust_ips:

Instead of secret_file you can give a list of IPs that are trusted. Trusted hosts can send pickle headers.

prefix:

This is used for explicitly configuring the value of SCRIPT_NAME. A request to /foo with prefix='/bar' will result in a SCRIPT_NAME of '/bar' and a PATH_INFO of '/foo'.

pop_prefix:

This is a prefix that is popped off the actual request path, and put into SCRIPT_NAME. A request to /bar/foo where pop_prefix='/bar' will result in a SCRIPT_NAME of '/bar' and a PATH_INFO of '/foo'.

scheme:

Force the scheme; e.g., to 'https'

host:

Force the host (including port!). You must give something like foo.com:80. This will replace the HTTP_HOST value, as well as SERVER_NAME and SERVER_PORT.

domain:

Force the domain (not including port). If you give foo.com it will rewrite HTTP_HOST to be foo.com:{port}, with whatever port was used for the actual request. Usually host will be more useful. Also SERVER_NAME will be set.

port:

Force the port (not including domain). If you give 80 it will set SERVER_PORT and the port portion of HTTP_HOST.

wsgiapp.spawn - Spawn subprocesses to handle requests

WSGI application that dispatches all requests to a subprocess.

The subprocess is managed by this process, started on demand (at the time of the first request) and closed when this process is closed.

See SpawningApplication for more.