您的位置:首页 > 移动开发 > Objective-C

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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐