【问题标题】:Python background process not writing to MySQL dbPython后台进程未写入MySQL数据库
【发布时间】:2013-04-20 19:31:57
【问题描述】:

如果之前有人问过这个问题,我深表歉意,但我无法找到有关此问题的任何记录。完全披露:我只使用了几个月的 Python 和大约 1 个月的 MySQL。

我在 Raspberry Pi(运行 Raspbian Wheezy)上编写了一个简短的 Python 脚本,它可以嗅探 wifi 数据包并将信号强度信息写入 MySQL 数据库。我还创建了一个小的 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)

谢谢! 凯尔

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

【问题讨论】:

  • 如果你能提供一些sn-p,你可以帮助我们帮助你。顺便说一句,我自己也在尝试做类似的事情,想知道你使用的是什么库。
  • 我很乐意这样做,@Nipun,我只是不确定我可以包含哪些内容会有所帮助。发布整个 python 脚本会不会有点矫枉过正?这是相当基本的,所以应该不会花很长时间通读。
  • 您可以将一些相关部分,例如推送到数据库的代码放在帖子中,并可能将整个代码链接到 Pastebin 上
  • 澄清一下:在前台运行时,您的代码可以正常工作并在数据库中插入数据?
  • 它在前台运行时就像一个魅力。虽然我认为在cur.execute() 之后添加con.commit() 解决了我的问题。我现在正在测试。

标签: python mysql linux


【解决方案1】:

我希望您不会在 db 事务上调用 commit

【讨论】:

  • 如果提交丢失,当没有像OP所说的那样在后台运行时,它如何将数据推送到数据库中
  • 我从未听说过commit,但我会调查一下。我已经编辑了我的问题以包含我的 python 脚本。
  • @NipunBatra 我在任何地方都没有看到 OP 这么说。
  • 我认为commit 解决了我的问题,我会在几分钟内回复结果!
  • @DanielRoseman:这句话让我很困惑:这个小系统的所有组件都按计划工作,但是......
猜你喜欢
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
相关资源
最近更新 更多