Python httplib、urllib 语法错误
我正在尝试运行最简单的 python 脚本来执行 http 请求并打印响应:
import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)
我尝试了这个“hello http”的许多变体,包括 httplib、urllib(2)、HTTPConnection 并将连接视为文件描述符。但我总是得到与此类似的错误代码:
./script.py: line 2: syntax error near unexpected token `('
./script.py: line 2: `f = urllib2.urlopen('http://www.python.org/')'
我无法弄清楚这一点。尝试在网络上搜索,但“附近的语法错误......”不足以获得正确的答案。
I'm trying to run the simplest python script to perform a http request and print the response:
import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)
And I tried many variation of this "hello http" including httplib, urllib(2), HTTPConnection and treating connection as a file descriptor. But I always end up with error code similar to this:
./script.py: line 2: syntax error near unexpected token `('
./script.py: line 2: `f = urllib2.urlopen('http://www.python.org/')'
I can't figure this out. Tried to search on web, but "syntaxe error near ..." isn't just enough to get proper answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记了脚本顶部的 shebang 。并确保删除 ImageMagick 创建的
urllib2
文件。You forgot the shebang at the top of the script. And make sure that you erase the
urllib2
file that ImageMagick has created.