Python后台进程不写入MySQL数据库

我很抱歉,如果这个问题之前已经问过,但是我找不到这个问题的任何logging。 完全披露:我只用了几个月的Python和MySQL大概一个月。

我在Raspberry Pi(运行Raspbian Wheezy)上编写了一个简短的Python脚本,它能够嗅探到无线数据包,并将信号强度信息写入MySQL数据库。 我还创build了一个小的PHP文件,从数据库中获取信息,并将其呈现在表格中(非常基本)。 这个小系统的所有组件完全按照计划工作,但是…

当我在后台运行Python脚本(sudo python my_script.py&)时,它不会以新的读数更新MySQL数据库。 然而,它也没有任何错误和输出到控制台没有问题(我每行都有一行打印一个wifi数据包被截断,其RSSI被添加到数据库)。 使用/etc/rc.local文件启动脚本时遇到同样的问题。 没有错误,但在数据库中没有更新。

是Python方面的问题? 一个MySQL设置,我需要改变? 还有什么我完全失踪?

编辑添加代码:

#!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb as mdb import sys from scapy.all import * # Create connection to MySQL database called 'DATABASE' at localhost with username 'USERNAME' and password 'PASSWORD' HOST = "localhost" USER = "USERNAME" PW = "PASSWORD" DB = "DATABASE" con = mdb.connect(HOST, USER, PW, DB) # set interface that will be used to monitor wifi interface = "mon0" with con: cur = con.cursor() # This function will be called every time a packet is intercepted. Packet is passed to function as 'p' def sniffmgmt(p): # These are the 3 types of management frames that are sent exclusively by clients (allows us to weed out packets sent by APs) stamgmtstypes = (0, 2, 4) if p.haslayer(Dot11): # Make sure packet is a client-only type if p.subtype in stamgmtstypes: # Calculate RSSI sig_str = -(256-(ord(p.notdecoded[-4:-3]))) # Update database with most recent detection cur.execute("REPLACE INTO attendance(mac_address, rssi, timestamp) VALUES('%s', %d, NOW())" % (p.addr2, sig_str)) # Print MAC address that was detected (debugging only) print "MAC Address (%s) has been logged" % (p.addr2) # Tell scapy what interface to use (see above) and which function to call when a packet is intercepted. lfilter limits packets to management frames. sniff(iface=interface, prn=sniffmgmt, store=0, lfilter=lambda x:x.type==0) 

谢谢! 凯尔

PS对于那些想知道的:这不是为了恶意使用,而是用来调查我们仓库的产品跟踪技术。

我希望你不要调用数据库事务commit