2013-03-17 46 views
1

我用网下代理服务器下代理服务器使用mwclient

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:[email protected]:8080' 
>>> handler = urllib2.ProxyHandler({'http':auth}) 
>>> opener = urllib2.build_opener(handler) 
>>> urllib2.install_opener(opener) 
+0

你认为mwclient怎么知道这个开启者?我没有使用mwclient,但看着它的代码,我认为它根本就不是urllib2。你能否详细说明,可能是发布了更多的代码 – dusual 2013-03-20 08:17:23

+0

mwclient使用httplib.HTTPConnection功能显示错误原因我没有代理环境 – Pradyumna 2013-03-20 10:46:32

+0

@perpetual:这是这里的问题;为了支持代理,你需要重写整个'mwclient'代码库。它不支持代理服务器。 – 2013-03-20 11:31:40

回答

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) 

然后我取代的下一个2次出现: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) 

它应该做的工作!