Python urllib.urlopen NameError: run from command line via PHP
Python urllib.urlopen NameError: run from command line via PHP
我正在 运行使用 popen
从 PHP window 创建一个 Python 脚本。在Python脚本中,我运行urllib.urlopen
。当 Internet Explorer、Safari 和 Firefox 中的 PHP 为 运行 时,此错误始终由 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
从未发生在GoogleChrome,我使用的是有效的URL.
我怀疑这会有帮助,但这里是使用的 Python,包含在 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)
网站托管在远程 HostGator 服务器上。
任何人都可以解释为什么会发生这种情况并提供解决方案吗?
如果您从 API 请求 json
,您不应该 eval
它,它会将其处理为 Python 代码。相反,您应该加载 json:
import json
data = json.load(urlopen(url))
目前出现您的问题是因为 JSON 中有一个 true
,它在 JSON 中有效,但在 Python 中无效 Python: ], boolean true 是 True
(注意大写 T
).
总的来说,eval
的想法很糟糕。当您不控制来源时更是如此。
我正在 运行使用 popen
从 PHP window 创建一个 Python 脚本。在Python脚本中,我运行urllib.urlopen
。当 Internet Explorer、Safari 和 Firefox 中的 PHP 为 运行 时,此错误始终由 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
从未发生在GoogleChrome,我使用的是有效的URL.
我怀疑这会有帮助,但这里是使用的 Python,包含在 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)
网站托管在远程 HostGator 服务器上。
任何人都可以解释为什么会发生这种情况并提供解决方案吗?
如果您从 API 请求 json
,您不应该 eval
它,它会将其处理为 Python 代码。相反,您应该加载 json:
import json
data = json.load(urlopen(url))
目前出现您的问题是因为 JSON 中有一个 true
,它在 JSON 中有效,但在 Python 中无效 Python: ], boolean true 是 True
(注意大写 T
).
总的来说,eval
的想法很糟糕。当您不控制来源时更是如此。