Python,Charles Severance博士撰写的网络程序

时间:2016-04-14 10:55:06

标签: python-2.7 network-programming

我是编程的初学者,从Python开始。我是Charles Severance博士的材料。所以在他的书中有一个例子:

import urllib
fhand = urllib.urlopen('http://www.py4inf.com/code/rom...
for line in fhand:
print line.strip()

当我将其粘贴到Python 2版本(我使用PyCharm 5.0.4)时,会出现:

Traceback (most recent call last):

File "D:/Python4yk/temprehg111.py", line 2, in <module>

fhand = urllib.urlopen('http://www.py4inf.com/code/rom...

File "C:\Python27\lib\urllib.py", line 87, in urlopen

return opener.open(url)

File "C:\Python27\lib\urllib.py", line 208, in open

return getattr(self, name)(url)

File "C:\Python27\lib\urllib.py", line 292, in open_http

import httplib

File "C:\Python27\lib\httplib.py", line 79, in <module>

import mimetools

File "C:\Python27\lib\mimetools.py", line 6, in <module>

import tempfile

File "C:\Python27\lib\tempfile.py", line 35, in <module>

from random import Random as _Random

File "random.py", line 3, in <module>

integers

NameError: name 'line' is not defined

当我输入另一个例子时,也会收到错误。怎么了?我甚至不写程序。我只是复制粘贴一个例子。问查克博士 - 仍然没有答案。

1 个答案:

答案 0 :(得分:0)

试试这个:

import urllib
fhand = urllib.urlopen('http://www.py4inf.com')
for line in fhand:
    print line.strip()       # notice the indentation