# 严格缩进要求,否则连接失败 with SSHTunnelForwarder( (ssh_host, ssh_port), ssh_username=ssh_user, ssh_password=ssh_password, remote_bind_address=(mysql_host, mysql_port)) as server: conn = pymysql.connect(host=mysql_host, port=server.local_bind_port, user=mysql_user, passwd=mysql_password, db=mysql_db)
cursor = conn.cursor() cursor.execute("select * from wp_statistics_visit where last_counter=\"" + time.strftime("%Y-%m-%d", time.localtime(time.time()))+"\"") data = cursor.fetchall()
for row in data: visit = row[3] cursor.execute("select * from wp_statistics_visitor where last_counter=\"" + time.strftime("%Y-%m-%d", time.localtime(time.time()))+"\"") data2 = cursor.fetchall() temp = 0 last = "" for row in data2: if row[0]>temp: temp = row[0] last = row[7] cursor.execute("select * from wp_statistics_useronline") data3 = cursor.fetchall() onlineNum = 0 platform = ""
if data3 !=pymysql.NULL: for row in data3: onlineNum+=1 platform=row[7] conn.commit() server.close() cursor.close() if conn != pymysql.NULL: print("conn is connect") return visit,last, onlineNum, platform if __name__ == "__main__": LCD.init_lcd() LCD.turn_light(1) LCD.print_lcd(0, 0, " Hello") LCD.print_lcd(0, 1, "Raspberry Pi") time.sleep(5) whileTrue: now = time.strftime("%m-%d %H:%M", time.localtime(time.time())) siteData = getVisit() LCD.clear_lcd() LCD.print_lcd(0, 0, 'TIME:'+now)#显示当前时间 LCD.print_lcd(0, 1, 'ONLINE:'+str(siteData[2])+" "+siteData[3]) time.sleep(5) LCD.clear_lcd() LCD.print_lcd(0, 0, 'VISIT:'+str(siteData[0])+" LAST:") LCD.print_lcd(0, 1, siteData[1]) time.sleep(5)