2013-03-17 72 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) 

它應該做的工作!