为什么要机械化.open()/ .read()/ .close()返回无/有套接字内部为无?

时间:2012-11-26 23:37:23

标签: python mechanize mechanize-python

我偶尔使用以下类型的代码AttributeError。我将mechanize实例设置为:

self.mech = mechanize.Browser(factory=mechanize.RobustFactory())
self.cj = mechanize.CookieJar()
self.mech.set_cookiejar(self.cj)
self.mech.set_proxies({'http': <snipped>})
self.mech.set_handle_robots(False)

USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
headers = [h for h in self.mech.addheaders if h[0].lower() != 'user-agent']
headers.append(('User-agent', USER_AGENT))
self.mech.addheaders = headers

我这样使用它:

resp = self.mech.open(the_url)
html = resp.read()
resp.close()

后一个片段偶尔会引发异常:

...
    html = resp.read()
AttributeError: 'NoneType' object has no attribute 'read'

在其他情况下,追溯实际上是:

...
    resp.close()
  File "C:\Python26\lib\site-packages\mechanize\_response.py", line 88, in close
    self.wrapped.close()
  File "C:\Python26\lib\site-packages\mechanize\_response.py", line 368, in close
    wrapped.close()
  File "C:\Python26\lib\socket.py", line 273, in close
    self._sock.close()
AttributeError: 'NoneType' object has no attribute 'close'

也就是说,.read()不会失败,但.close()会失败。更多追溯:

...
    html = resp.read()
  File "C:\Python26\lib\site-packages\mechanize\_response.py", line 190, in read
    self.__cache.write(self.wrapped.read())
  File "C:\Python26\lib\socket.py", line 348, in read
    data = self._sock.recv(rbufsize)
  File "C:\Python26\lib\httplib.py", line 542, in read
    s = self.fp.read(amt)
  File "C:\Python26\lib\socket.py", line 377, in read
    data = self._sock.recv(left)
error: [Errno 10035] A non-blocking socket operation could not be completed immediately

    resp = self.mech.open(the_url)
  File "C:\Python26\lib\site-packages\mechanize\_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "C:\Python26\lib\site-packages\mechanize\_mechanize.py", line 249, in _mech_open
    self._set_response(response, False)
  File "C:\Python26\lib\site-packages\mechanize\_mechanize.py", line 308, in     _set_response
    self._factory.set_response(response)
  File "C:\Python26\lib\site-packages\mechanize\_html.py", line 623, in set_response
    data = response.read()
  File "C:\Python26\lib\site-packages\mechanize\_response.py", line 190, in read
    self.__cache.write(self.wrapped.read())
  File "C:\Python26\lib\socket.py", line 348, in read
    data = self._sock.recv(rbufsize)
  File "C:\Python26\lib\httplib.py", line 542, in read
    s = self.fp.read(amt)
  File "C:\Python26\lib\socket.py", line 377, in read
    data = self._sock.recv(left)
AttributeError: 'NoneType' object has no attribute 'recv'

为什么会发生这种情况?机械化文档不是很好,光源的光标显示它相对复杂。

0 个答案:

没有答案