2016-12-29 174 views
-2

我需要为Python 3反向代理3

我喜欢扭曲和其简单的反向HTTP代理(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() 

....但它在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 

是否有类似的东西在python 3中起作用?

回答