Monday, May 6, 2024
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 23967  / 3 Years ago, thu, october 28, 2021, 8:24:15

While running the simple Python program below, I'm getting the following error:



./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.

More From » command-line

 Answers
5

You missed to add a shebang at the beginning of the script to tell the shell to interpret your script as a python script. Something like this:



#!/usr/bin/python


or:



#!/usr/bin/env python


See also: Why do people write #!/usr/bin/env python on the first line of a Python script?


[#29318] Saturday, October 30, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
urvedcaly

Total Points: 171
Total Questions: 118
Total Answers: 124

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
urvedcaly questions
Tue, Nov 8, 22, 19:40, 2 Years ago
Fri, May 26, 23, 15:27, 1 Year ago
Wed, Jan 19, 22, 08:09, 2 Years ago
Thu, May 13, 21, 03:34, 3 Years ago
;