'Output to CGI using Python?? Error keeps telling me missing parenthesis?

Attempting to generate using python to screen using cgi. However, when I run it from the command line, I keep getting an error stating that it is missing parenthesis on the line print "Content-type:text/html\r\n\r\n".

#!/usr/bin/python3
import urllib.request
import json
import os

link = "https://api.nasa.gov/planetary/apod?api_key....."
resp = urllib.request.urlopen(link)
data = resp.read()
print(str(data, 'utf-8'))
returnJson = json.loads(data)
img_url = returnJson['url']
title = returnJson['title']
current_date = returnJson['date']

(filename, headers) = urllib.request.urlretrieve(img_url)
img_file_name = img_url.split('/')[-1]
os.rename(filename, img_file_name)
html = """
<center>
       <h1>Astronomy Picture of the Day</h1>
       <img src="%s">
       <p><b>%s</b></p>
</center>
""" % (img_file_name, title)
html_file_name = 'nasa_apod_%s.html' %current_date

print "Content-type:text/html\r\n\r\n" **Where it says parenthesis**
print '<html>'
print '<head>'
print '<title>Astronomy Picture of the Day</title>'
print '</head>'
print '<body>'
print '<h1>Astronomy Picture of the Day</h1>'
print '</body>'
print '</html>'


Solution 1:[1]

This is because you are using python 3. In python 3 print is a function not a statement. So this means you need to add parentheses around anything you print.

# this will fail in Python 3
print "Content-type:text/html\r\n\r\n"

# but this will work
print("Content-type:text/html\r\n\r\n")

As you do, earlier on with print(str(data, 'utf-8'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JamesDerrick