我在python中有一个函数可以从数据库中获取ip,我想遍历它们并调用ping函数,但是在主题区域中我得到一个错误“canoncatenatedtuple(not”str“)to tuple error”。当我说print调用函数show(ipps)时,它只打印数组中的最后一个值
json转储返回
{"ip addresses": [["192.168.0.11"], ["192.168.0.12"], ["192.168.0.13"], ["192.168.0.15"]]}
@ps_gaming.route('/getPSStatusping')
def getPSStatusping():
try:
if session.get('id'):
con = mysql.connect()
cursor = con.cursor()
cursor.callproc('sp_getPSIPSstats')
all_my_ps = cursor.fetchall()
for ipps in all_my_ps:
show(ipps) //error is thrown here
return json.dumps({"ip_addresses":all_my_ps})
else:
return render_template('error.html', error='Unauthorized Access')
except Exception as e:
return render_template('error.html', error=str(e))
finally:
cursor.close()
con.close()
其他功能
^{pr2}$有什么建议吗
在
show
中,hostname1
是一个元组。您尝试向其添加字符串,因此出现错误。您需要将hostname1
转换为字符串,或者从中提取字符串值。我的猜测是,hostname1
元组只包含一个值,因此您可以使用类似print hostname1[0] + ' Is Active'
的值hostname1
是一个元组,因为cursor.fetchall()
将返回一个元组(行)的元组(列),而您将迭代外部元组。在每次迭代中,ipps
本身就是一个元组(不过,可能只有一个元素在这个元组中)。即使每一行显然只包含一个列,查询仍然以元组的形式返回每一行。当您将其转换为json时,它被表示为一个嵌套列表。在在您的
show
函数中,尝试将hostname1
转换为字符串,即doprint str(hostname1) + ' is active'
,同样地,对于不可访问的相关问题 更多 >
编程相关推荐