При запуске простой программы на Python, приведенной ниже, я получаю следующую ошибку:
./url_test.py: line 2: syntax error near unexpected token `('
./url_test.py: line 2: `response = urllib2.urlopen('http://python.org/')'
import urllib2
response = urllib2.urlopen('http://python.org/')
print "Response:", response
# Get the URL. This gets the real URL.
print "The URL is: ", response.geturl()
# Getting the code
print "This gets the code: ", response.code
# Get the Headers.
# This returns a dictionary-like object that describes the page fetched,
# particularly the headers sent by the server
print "The Headers are: ", response.info()
# Get the date part of the header
print "The Date is: ", response.info()['date']
# Get the server part of the header
print "The Server is: ", response.info()['server']
# Get all data
html = response.read()
print "Get all data: ", html
# Get only the length
print "Get the length :", len(html)
# Showing that the file object is iterable
for line in response:
print line.rstrip()
# Note that the rstrip strips the trailing newlines and carriage returns before
# printing the output.
Вы пропустили добавление шебанга в начале скрипта, чтобы сказать оболочке интерпретировать ваш скрипт как скрипт на python. Примерно так:
#!/usr/bin/python
или:
#!/usr/bin/env python
См. Также: Почему люди пишут #! / Usr / bin / env python в первой строке Python скрипт?
Эти ошибки не являются типичными трассировками Python. Эти ошибки выглядят как ошибки оболочки.
Я думаю, что вы пытаетесь запустить скрипт Python с Bash (или другой оболочкой), т.е. вы делаете что-то вроде:
bash url_test.py
Вместо этого вы должны использовать:
python url_test.py