【问题标题】:How to make a proxy mechanism in pyramid framework?如何在金字塔框架中制作代理机制?
【发布时间】:2016-02-14 21:30:38
【问题描述】:

我有以下问题: 有带有金字塔应用程序的服务器,他可以从互联网上看到和访问。 有带有地图的服务器,从互联网上看不到(两台服务器都在一个本地网络中)。 应用服务器从地图服务器请求一些数据,但是这些请求是用javascript编写的,它运行在客户端的机器上,请求是由ip定向的,当然是本地的(比如192.168.1.28),从我们的本地网络用户可以看到地图,但来自互联网不能。 如果客户端请求映射,如何制作工具(可能是附加视图或类似的东西)将请求重定向到映射,以便金字塔服务器像代理一样工作。

像这样: 来自互联网的客户端 金字塔地图服务器

【问题讨论】:

  • 您正在寻找的是一个反向代理,它可以将请求代理到您的后端地图服务器。

标签: javascript python proxy pyramid


【解决方案1】:

我假设您想使用 Pyramid 应用程序构建 HTTP 代理,尽管您不完全清楚为什么要使用 Pyramid 执行此操作,或者它是这里的最佳解决方案。

Pyramid 和大多数现代 Python Web 应用程序使用 WSGI 标准来解码和处理 HTTP 请求。下面我提出了一个适用于所有 WSGI 服务器的解决方案。这是一个稍作修改的wsgi-proxy application。该示例的完整源代码和实现可在in pyramid_notebook 获得。此实现仅允许代理到不同的localhost 端口,但可以对其进行更新以允许任意 Intranet 源 IP。

您可以编写一个 Pyramid 视图来捕获对某个路径的所有请求,例如/map-to-internal-server:

def proxy_it(request, port):
    """Proxy HTTP request to upstream server."""
    proxy_app = WSGIProxyApplication(port)
    return request.get_response(proxy_app)

然后你可以拥有 WSGIProxyApplication 来执行连接上游服务器并将响应向下流式传输到客户端的逻辑:

# Courtesy of https://bitbucket.org/dahlia/wsgi-proxy/raw/02ab0dfa8e0078add268e91426e1cc1a52664cf5/wsgi_proxy/__init__.py

import http.client
import logging
from urllib.parse import urlparse, urlunsplit, urlunparse, unquote_plus


#: (:class:`frozenset`) The set of hop-by-hop headers.  All header names
#: all normalized to lowercase.
HOPPISH_HEADERS = frozenset([
    'keep-alive', 'proxy-authenticate',
    'proxy-authorization', 'te', 'trailers', 'transfer-encoding',
    'proxy-connection'
    # "upgrade", "connection"
])


def is_hop_by_hop(header):
    """Returns :const:`True` if the given ``header`` is hop by hop.

    :param header: the header name
    :type header: :class:`basestring`
    :returns: whether the given ``header`` is hop by hop or not
    :rtype: :class:`bool`

    """
    return header.lower() in HOPPISH_HEADERS


def reconstruct_url(environ, port):
    """Reconstruct the remote url from the given WSGI ``environ`` dictionary.

    :param environ: the WSGI environment
    :type environ: :class:`collections.MutableMapping`
    :returns: the remote url to proxy
    :rtype: :class:`basestring`

    """
    # From WSGI spec, PEP 333
    url = environ.get('PATH_INFO', '')
    if not url.startswith(('http://', 'https://')):
        url = '%s://%s%s' % (
            environ['wsgi.url_scheme'],
            environ['HTTP_HOST'],
            url
        )
    # Fix ;arg=value in url
    if '%3B' in url:
        url, arg = url.split('%3B', 1)
        url = ';'.join([url, arg.replace('%3D', '=')])
    # Stick query string back in
    try:
        query_string = environ['QUERY_STRING']
    except KeyError:
        pass
    else:
        url += '?' + query_string


    parsed = urlparse(url)
    replaced = parsed._replace(netloc="localhost:{}".format(port))
    url = urlunparse(replaced)
    environ['reconstructed_url'] = url
    return url


class WSGIProxyApplication:
    """WSGI application to handle requests that need to be proxied.
    You have to instantiate the class before using it as WSGI app::

        from wsgiref.simple_server import make_server

        app = WSGIProxyApplication()
        make_server('', 8080, app).serve_forever()

    """

    #: (:class:`types.ClassType`) The connection class of :mod:`httplib` module.
    #: It should be a subtype of :class:`httplib.HTTPConnection`.
    #: Default is :class:`httplib.HTTPConnection`.
    connection_class = http.client.HTTPConnection

    def __init__(self, port):
        # Target port where we proxy IPython Notebook
        self.port = port

    def handler(self, environ, start_response):
        """Proxy for requests to the actual http server"""
        logger = logging.getLogger(__name__ + '.WSGIProxyApplication.handler')
        url = urlparse(reconstruct_url(environ, self.port))

        # Create connection object
        try:
            connection = self.connection_class(url.netloc)
            # Build path
            path = url.geturl().replace('%s://%s' % (url.scheme, url.netloc),
                                        '')
        except Exception:
            start_response('501 Gateway Error', [('Content-Type', 'text/html')])
            logger.exception('Could not Connect')
            yield '<H1>Could not connect</H1>'
            return

        # Read in request body if it exists
        body = length = None
        try:
            length = int(environ['CONTENT_LENGTH'])
        except (KeyError, ValueError):

            # This is a situation where client HTTP POST is missing content-length.
            # This is also situation where (WebOb?) may screw up encoding and isert extranous = in the body.
            # https://github.com/ipython/ipython/issues/8416
            if environ["REQUEST_METHOD"] == "POST":
                if environ.get("CONTENT_TYPE") == 'application/x-www-form-urlencoded; charset=UTF-8':
                    body = environ['wsgi.input'].read()
                    try:
                        body = unquote_plus(body.decode("utf-8"))

                        # Fix extra = at end of JSON payload
                        if body.startswith("{") and body.endswith("}="):
                            body = body[0:len(body)-1]

                    except Exception as e:
                        logger.exception(e)
                        logger.error("Could not decode body: %s", body)

                    length = len(body)
        else:
            body = environ['wsgi.input'].read(length)

        # Build headers
        logger.debug('environ = %r', environ)
        headers = dict(
            (key, value)
            for key, value in (
                # This is a hacky way of getting the header names right
                (key[5:].lower().replace('_', '-'), value)
                for key, value in environ.items()
                # Keys that start with HTTP_ are all headers
                if key.startswith('HTTP_')
            )
            if not is_hop_by_hop(key)
        )

        # Handler headers that aren't HTTP_ in environ
        try:
            headers['content-type'] = environ['CONTENT_TYPE']
        except KeyError:
            pass

        # Add our host if one isn't defined
        if 'host' not in headers:
            headers['host'] = environ['SERVER_NAME']

        # Make the remote request
        try:

            logger.debug('%s %s %r',
                         environ['REQUEST_METHOD'], path, headers)
            connection.request(environ['REQUEST_METHOD'], path,
                               body=body, headers=headers)
        except Exception as e:
            # We need extra exception handling in the case the server fails
            # in mid connection, it's an edge case but I've seen it
            logger.exception(e)
            start_response('501 Gateway Error', [('Content-Type', 'text/html')])
            yield '<H1>Could not proxy IPython Notebook running localhost:{}</H1>'.format(self.port).encode("utf-8")
            return

        response = connection.getresponse()

        hopped_headers = response.getheaders()
        headers = [(key, value)
                   for key, value in hopped_headers
                   if not is_hop_by_hop(key)]

        start_response('{0.status} {0.reason}'.format(response), headers)
        while True:
            chunk = response.read(4096)
            if chunk:
                yield chunk
            else:
                break

    def __call__(self, environ, start_response):
        return self.handler(environ, start_response)

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 2014-05-11
    • 1970-01-01
    • 2023-01-25
    • 2022-11-15
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多