在代理服务器下使用mwclient

时间:2013-03-17 13:18:24

标签: python proxy

我在代理服务器下使用net

 self.wp = site if site else mwclient.Site(self.url)

当遇到以上错误时遇到上述行

 File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\client.py", line 92, in __init__
    self.site_init()
  File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\client.py", line 100, in site_init
    siprop = 'general|namespaces', uiprop = 'groups|rights')
  File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\client.py", line 165, in api
    info = self.raw_api(action, **kwargs)
  File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\client.py", line 248, in raw_api
    json_data = self.raw_call('api', data).read()
  File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\client.py", line 223, in raw_call
    url, data = data, headers = headers)
  File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\http.py", line 225, in post
    return self.find_connection(host).post(host,
  File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\http.py", line 218, in find_connection
    conn = cls(host, self)
  File "C:\Python27\lib\site-packages\mwclient-0.6.5-py2.7.egg\mwclient\http.py", line 62, in __init__
    self._conn.connect()
  File "C:\Python27\lib\httplib.py", line 757, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 571, in create_connection
    raise err
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我尝试通过以下步骤使用urllib2设置代理,但它没有帮助

>>> import urllib2
>>> auth = 'http://xxxxx:xxxx@10.1.9.30:8080'
>>> handler = urllib2.ProxyHandler({'http':auth})
>>> opener = urllib2.build_opener(handler)
>>> urllib2.install_opener(opener)

1 个答案:

答案 0 :(得分:0)

这有点旧,但昨天我遇到了同样的问题,我在这里发布解决方案,因为它可能对其他人有帮助。

我设法通过更改文件mwclinet / http.py来对其进行排序。基本上我检查环境变量http_proxy是否存在并通过代理而不是直接连接。

在课程class HTTPPersistentConnection(object):中,我添加了一个变量usesProxy = False。在第61行附近,我将self._conn = self.http_class(host)替换为:

    http_proxy_env = os.environ.get('http_proxy')
    if http_proxy_env is not None:
      try:
        # proxy
        http_proxy_url = urlparse.urlparse(http_proxy_env)
        http_proxy_host,http_proxy_port = http_proxy_url.netloc.split(':')
        self._conn = self.http_class(http_proxy_host,int(http_proxy_port))
        self.usesProxy=True;
      except:
        self._conn = self.http_class(host)
    else: 
      self._conn = self.http_class(host)

然后我替换了下两次出现的self._conn.request(method, path, ....)。 在第94行:

  if self.usesProxy is False:
    self._conn.request(method, path, headers = headers)
  else:
    self._conn.request(method, "http://"+host+path, headers = headers)

和第107行:

if self.usesProxy is False:
  self._conn.request(method, path, data, headers)
else:
  self._conn.request(method, "http://"+host+path, data, headers)

它应该做的工作!