I'm trying to create webproxy for personal use to access facebook (it is often blocked from a few locations where I happen to spend some time).
Started from this code: http://code.google.com/p/gevent/source/browse/examples/webproxy.py?name=1.0b2
I've modified it to work with the cookies replacing urllib with requests in the following way:
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]
But when I try to login I've got the following error:
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"
My guess with happens because of some problem while creating WSGI response but I could figure out exactly why.