I am running a Python script from a PHP window using popen
. In the Python script, I run urllib.urlopen
. When the PHP is run from Internet Explorer, Safari, and Firefox, this error is always thrown by urllib.urlopen
:
Error: ['Traceback (most recent call last):', ' File "/home4/pantano/public_html/software/eco/python/query.py", line 29, in geocode\n data = eval(urlopen(url).read())\n', ' File "", line 148, in \n', "NameError: name 'true' is not defined\n"].
#straight from traceback module
This never happens in Google Chrome, and I am using a valid URL.
I doubt this will help, but here is the Python used, contained inside query.py
:
def geocode(self, address, console, num, outof, attempt):
'''Returns LatLng of Geocoding API'''
if attempt == self.max_failed_queries:
console.add('elevation', num, outof, False, 'Quitting this query. Data will be inaccurate')
return None, False
url = 'https://maps.googleapis.com/maps/api/geocode/json?address='+address.replace(' ','+',len(address))+'&key=AIzaSyCnHT7IpJu0O7n-apLNW8iKkW_rTIuANuE'
current_time = time.time()
if current_time < self.next_query_time: #before earliest possible query time
time.sleep(self.next_query_time - current_time) #wait until next query time
try:
data = eval(urlopen(url).read())
self.next_query_time = current_time + 0.2
if data['status'] == 'OK':
console.add('geocode', num, outof)
return LatLng(float(data['results'][0]['geometry']['location']['lat']), float(data['results'][0]['geometry']['location']['lng'])), True
else:
console.add('geocode', num, outof, False, 'Problem with query or data: '+data['status'])
return self.geocode(address, console, num, outof, attempt+1)
except:
type_, value_, traceback_ = sys.exc_info()
console.add('geocode', num, outof, False, str(traceback.format_exception(type_, value_, traceback_)))
return self.geocode(address, console, num, outof, attempt+1)
The website is hosted on a remote HostGator server.
Could anyone please explain why this is happening and also provide a fix for this?
If you're requesting
json
from an API, you shouldn'teval
it, which processes it as Python code. Instead, you should load the json:Currently your issue is coming up because there's a
true
in the JSON, which is valid JSON, but not valid Python: in Python, boolean true isTrue
(note the uppercaseT
).Generally speaking, it's a terrible, terrible, terrible idea to
eval
stuff. Even more so when you don't control the source.