python : 'NoneType' object has no attribute 'recv'
2014-12-31 09:57
295 查看
google = urllib.urlopen('http://www.google.com')
print 'http header:/n', google.info()
print 'http status:', google.getcode()
print 'url:', google.geturl()
for line in google: # 就像在操作本地文件
print line,
google.close()
学习如上的代码,发现错误:原因是google.close()对齐错误,导致循环第一次就将文件举例关闭,后续无法访问该文件举例。
另一个错误是 打印提示无info,getcode属性。错误原因是用了文件举例的read方法,获取的网页不再文件句柄而是HTML文件。
ppeix:urllib2$ python urltest.py
http header:
Date: Wed, 31 Dec 2014 01:29:24 GMT
Server: Apache
Cache-Control: max-age=86400
Expires: Thu, 01 Jan 2015 01:29:24 GMT
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-4b4c7d90"
Accept-Ranges: bytes
Content-Length: 81
Connection: Close
Content-Type: text/html
http status:
200
url: http://baidu.com
<html>
Traceback (most recent call last):
File "urltest.py", line 13, in <module>
for line in google: # 就像在操作本地文件
File "/usr/lib/python2.7/socket.py", line 530, in next
line = self.readline()
File "/usr/lib/python2.7/socket.py", line 426, in readline
recv = self._sock.recv
AttributeError: 'NoneType' object has no attribute 'recv'
print 'http header:/n', google.info()
print 'http status:', google.getcode()
print 'url:', google.geturl()
for line in google: # 就像在操作本地文件
print line,
google.close()
学习如上的代码,发现错误:原因是google.close()对齐错误,导致循环第一次就将文件举例关闭,后续无法访问该文件举例。
另一个错误是 打印提示无info,getcode属性。错误原因是用了文件举例的read方法,获取的网页不再文件句柄而是HTML文件。
ppeix:urllib2$ python urltest.py
http header:
Date: Wed, 31 Dec 2014 01:29:24 GMT
Server: Apache
Cache-Control: max-age=86400
Expires: Thu, 01 Jan 2015 01:29:24 GMT
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-4b4c7d90"
Accept-Ranges: bytes
Content-Length: 81
Connection: Close
Content-Type: text/html
http status:
200
url: http://baidu.com
<html>
Traceback (most recent call last):
File "urltest.py", line 13, in <module>
for line in google: # 就像在操作本地文件
File "/usr/lib/python2.7/socket.py", line 530, in next
line = self.readline()
File "/usr/lib/python2.7/socket.py", line 426, in readline
recv = self._sock.recv
AttributeError: 'NoneType' object has no attribute 'recv'
相关文章推荐
- python PIL bug: 'NoneType' object has no attribute 'bands' (解决)
- python使用PIL库报错,'NoneType' object has no attribute 'bands'
- 《python核心编程》序列:字符串、列表和元组Python: Attribute Error - 'NoneType' object has no attribute
- [Python][转]Exception AttributeError: "'NoneType' object has no attribute
- python __def__ Exception AttributeError: "'NoneType' object has no attribute
- 使用Buildozer部署时,出现AttributeError: 'NoneType' object has no attribute 'group'错误的解决
- scapy AttributeError: 'NoneType' object has no attribute 'route'解决办法
- AttributeError: 'NoneType' object has no attribute 'clone'
- TypeError: 'generator' object has no attribute '__getitem__'
- 'module' object has no attribute 'getppid' odoo openerp debug报错
- Python基础学习-'module' object has no attribute 'urlopen'解决方法
- [python]Django AttributeError 'tuple' object has no attribute 'regex'
- 'SafeUnicode' object has no attribute 'status_code'
- python AttributeError: 'module' object has no attribute 'setdefaultencoding'
- 'WSGIRequest' object has no attribute 'user'
- 当使用JQuery的"$",抛异常Uncaught TypeError: Object #<Object> has no method 'ajax'
- 'module' object has no attribute 'rindex'
- 'generator' object has no attribute '__getitem__'
- python遇到AttributeError: 'str' object has no attribute '_getAssertEqualityFunc'问题解决过程
- AttributeError: type object '_socketobject' has no attribute 'socket'