Python background process not writing to MySQL db
Question: I apologize if this question has been asked before, but I was unable to find
any record of this issue. Full disclosure: I've only been using Python for a
few months and MySQL for about 1 month.

I've written a short Python script on a Raspberry Pi (running Raspbian Wheezy)
that sniffs wifi packets and writes signal strength info to a MySQL database.
I've also created a small PHP file that grabs the info from the database and
presents it in a table (pretty basic). All components of this little system
work exactly as planned, however...

When I run the Python script in the background (sudo python my_script.py &) it
does not appear to update the MySQL database with new readings. Yet it also
throws no errors and outputs to the console without a problem (I have a line
printed each time a wifi packet is intercepted and its RSSI is added to the
database). I encountered the same problem when starting the script at boot up
using the /etc/rc.local file. No errors, but no updates in the database
either.

Is the problem on the Python side of things? A MySQL setting that I need to
change? Is there something else I'm completely missing?

EDITED TO ADD CODE:

    
    
    #!/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)
    

Thanks! Kyle

P.S. For those who are wondering: this is not intended for malicious use, it
is being used to investigate product tracking techniques at our warehouse.


Answer: I expect you're not calling `commit` on the db transaction.

