我正在尝试创建供个人使用的 webproxy 以访问 facebook(它经常被我碰巧花了一些时间的几个位置阻止)。
从此代码开始:http ://code.google.com/p/gevent/source/browse/examples/webproxy.py?name=1.0b2
我已对其进行了修改,以使用以下方式将 urllib 替换为请求的 cookie:
def proxy_post(path, env, proxy_url, start_response):
if '://' not in path:
path = 'http://' + path
try:
#response = br.submit(path, env)
response = requests.post(path, params = env)
print '%s: %s' % (path, response)
headers = [(k, v) for (k, v) in response.headers.items() if k not in drop_headers]
scheme, netloc, path, params, query, fragment = urlparse(path)
host = (scheme or 'http') + '://' + netloc
except Exception, ex:
sys.stderr.write('error while reading %s:\n' % path)
traceback.print_exc()
tb = traceback.format_exc()
error_str = escape(str(ex) or ex.__class__.__name__ or 'Error')
return ['<h1>%s</h1><h2>%s</h2><pre>%s</pre>' % (error_str, escape(path), escape(tb))]
else:
start_response('%s OK' % response.status_code, headers)
data = response.content
data = fix_links(data, proxy_url, host)
return [data]
但是当我尝试登录时,出现以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent/wsgi.py", line 116, in handle
self.data.extend(result)
TypeError: 'NoneType' object is not iterable
<WSGIServer fileno=3 address=0.0.0.0:8088>: Failed to handle request:
request = <http_request "POST /https://www.facebook.com/login.php?login_attempt=1 HTTP/1.1" 127.0.0.1:56381>
application = <function application at 0x2be2578>
127.0.0.1 - - [2013-04-13 20:08:03] "POST /https://www.facebook.com/login.php?login_attempt=1 HTTP/1.1" 500 21 "http://127.0.0.1:8088/http://www.facebook.com" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0"
我的猜测是因为在创建 WSGI 响应时出现了一些问题,但我可以弄清楚原因。