1

I need a simple reverse proxy for Python 3.

I like Twisted and its simple reverse http proxy (http://twistedmatrix.com/documents/14.0.1/_downloads/reverse-proxy.py) ...

# 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:8080/ in your web browser.
"""

from twisted.internet import reactor
from twisted.web import proxy, server

site = server.Site(proxy.ReverseProxyResource('www.yahoo.com', 80, ''))
reactor.listenTCP(8080, site)
reactor.run()

.... but it throws error in Python 3.

Unhandled Error
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/twisted/protocols/basic.py", line 571, in dataReceived
    why = self.lineReceived(line)
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/http.py", line 1752, in lineReceived
    self.allContentReceived()
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/http.py", line 1845, in allContentReceived
    req.requestReceived(command, path, version)
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/http.py", line 766, in requestReceived
    self.process()
--- <exception caught here> ---
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/server.py", line 185, in process
    resrc = self.site.getResourceFor(self)
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/server.py", line 791, in getResourceFor
    return resource.getChildForRequest(self.resource, request)
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/resource.py", line 98, in getChildForRequest
    resource = resource.getChildWithDefault(pathElement, request)
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/resource.py", line 201, in getChildWithDefault
    return self.getChild(path, request)
  File "/usr/local/lib/python3.4/dist-packages/twisted/web/proxy.py", line 278, in getChild
    self.host, self.port, self.path + b'/' + urlquote(path, safe=b"").encode('utf-8'),
builtins.TypeError: Can't convert 'bytes' object to str implicitly

Is there something similar that works in python 3?

2 Answers 2

4

Having stumbled into this problem myself, I was disappointed to find no answer here. After some digging, I was able to get it working by changing the path argument in proxy.ReverseProxyResource to bytes rather than str, rendering the following line:

site = server.Site(proxy.ReverseProxyResource("www.yahoo.com", 80, b''))

This is necessary because twisted appends a trailing slash as bytes (ie b'/') internally.

1

I found this simple package on pypi, it seems to be working well and it is similarly simple.

https://pypi.python.org/pypi/maproxy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.