0

我正在尝试创建供个人使用的 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 响应时出现了一些问题,但我可以弄清楚原因。

4

1 回答 1

0

我看了你提到的webproxy 。里面def application(env, start_response):有以下代码:

 elif (method, path) == ('POST', ''):
    key, value = env['wsgi.input'].read().strip().split('=')
    assert key == 'url', repr(key)
    start_response('302 Found', [('Location', join(proxy_url, unquote(value)))])
elif method == 'POST':
    start_response('404 Not Found', [])

因此,如果方法是POST并且路径不为空404,则返回。根据您附加的回复,您正在使用POST带有路径的方法,/https://www.facebook.com/login.php?login_attempt=1我将在那里开始搜索。这可能与urllib2.open()和返回的结果之间存在差异mechanize.Browser().open()

旁注:那条路径是错误的,它不应该以 / https://www.facebook.com/开头。

于 2013-04-12T10:39:11.337 回答