两个同时的 Python 循环产生一个结果

发布于 2024-12-07 08:55:07 字数 1212 浏览 0 评论 0原文

我目前有一段 Python 2.6 代码,它同时运行两个循环。该代码使用了gps(gpsd)模块和scapy模块。基本上,第一个函数 (gpsInfo) 包含一个连续的 while 循环,从 GPS 设备获取 GPS 数据并将位置写入控制台。第二个函数 (ClientDetect) 在连续循环中运行,还会嗅探空气中的 wifi 数据,并在找到特定数据包时打印此数据。我已将这两个循环与 GPS 循环作为后台线程运行。我想要做的(并且已经努力了 5 天来弄清楚如何做)是,当 ClientDetect 函数找到匹配项并打印相应的信息时,我希望将命中时的相应 GPS 坐标也打印到安慰。目前我的代码似乎不起作用。

observedclients = [] p = ""  # Relate to wifi packet session =
gps.gps(mode=gps.WATCH_NEWSTYLE)

def gpsInfo():

    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            print session.fix.latitude + session.fix.longitude
            time.sleep(0.1)

def WifiDetect(p):
    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                print p.addr2
                observedclients.append(p.addr2)  

def ActivateWifiDetect():
    sniff(iface="mon0", prn=WifiDetect)

if __name__ == '__main__':
    t = threading.Thread(target=gpsInfo)
    t.start()
    WifiDetect()

任何人都可以查看我的代码,看看如何最好地在连接 WiFi 时同时获取数据,以便打印 GPS 坐标?有人提到实现排队,但我对此进行了研究,但对于如何实现它却无济于事。

如前所述,此代码的目的是扫描 GPS 和特定 wifi 数据包,并在检测到时打印与数据包相关的详细信息以及检测到的位置。

I currently have a Python 2.6 piece of code that runs two loops simultaneously. The code uses the gps (gpsd) module and the scapy module. Basically the first function (gpsInfo) contains a continual while loop grabbing GPS data from a GPS device and writing the location to console. The second function (ClientDetect) runs in a continual loop also sniffs the air for wifi data and prints this data when specific packets are found. I've threaded these two loops with the GPS one running as a background thread. What I am looking to do (and have been struggling for 5 days to work out how) is for, when the ClientDetect function finds a match and prints the respective info, I want the respective GPS coordinates of when that hit was made also printed to console. At present my code doesn't seem to work.

observedclients = [] p = ""  # Relate to wifi packet session =
gps.gps(mode=gps.WATCH_NEWSTYLE)

def gpsInfo():

    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            print session.fix.latitude + session.fix.longitude
            time.sleep(0.1)

def WifiDetect(p):
    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                print p.addr2
                observedclients.append(p.addr2)  

def ActivateWifiDetect():
    sniff(iface="mon0", prn=WifiDetect)

if __name__ == '__main__':
    t = threading.Thread(target=gpsInfo)
    t.start()
    WifiDetect()

Can anybody look at my code to see how best to grab the data simultaneously for when there is a wifi hit, for the GPS coordinates to be printed too? Somebody mentioned implementing queuing but I have researched this but to no avail with regards to how to implement it.

As said, the aim of this code is to scan for both GPS and specific wifi packets and when detected, print details relating to the packet and the location where is was detected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青萝楚歌 2024-12-14 08:55:07

获得此信息的一个简单方法是将 GPS 位置存储在全局变量中,并让 wifi 嗅探线程在需要打印某些数据时读取该全局变量;问题是,由于两个线程可以同时访问全局变量,因此您需要用互斥锁来包装它;

last_location = (None, None)
location_mutex = threading.Lock()

def gpsInfo():
    global last_location
    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            with location_mutex:
                # DON'T Print from inside thread!
                last_location = session.fix.latitude, session.fix.longitude
            time.sleep(0.1)

def WifiDetect(p):
    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                with location_mutex:
                    print p.addr2, last_location
                    observedclients.append((p.addr2, last_location))  

A simple way of getting this is store the gps location in a global variable, and have the wifi sniffing thread read that global when it needs to print some data; The gotcha is that since two threads can be accessing the global variable at the same time, you'll want to wrap it with a mutex;

last_location = (None, None)
location_mutex = threading.Lock()

def gpsInfo():
    global last_location
    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            with location_mutex:
                # DON'T Print from inside thread!
                last_location = session.fix.latitude, session.fix.longitude
            time.sleep(0.1)

def WifiDetect(p):
    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                with location_mutex:
                    print p.addr2, last_location
                    observedclients.append((p.addr2, last_location))  
南城追梦 2024-12-14 08:55:07

当你在函数中使用 gps 时,你需要告诉 python 你正在使用外部变量。代码应该如下所示:

def gpsInfo():
    global gps # new line

    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            print session.fix.latitude + session.fix.longitude
            time.sleep(0.1)


def WifiDetect(p):
    global p, observedclients # new line

    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                print p.addr2
                observedclients.append(p.addr2)  

you need to tell python you are using an external variable when you use gps in a function. The code should look like this:

def gpsInfo():
    global gps # new line

    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            print session.fix.latitude + session.fix.longitude
            time.sleep(0.1)


def WifiDetect(p):
    global p, observedclients # new line

    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                print p.addr2
                observedclients.append(p.addr2)  
浅笑轻吟梦一曲 2024-12-14 08:55:07

我认为你应该更具体地确定你的目标。

如果您只想在嗅探 Wifi 网络时获取 GPS 坐标,只需执行(伪代码):

while True:
    if networkSniffed():
        async_GetGPSCoords()

如果您想要所有 GPS 坐标的日志并希望将其与 Wifi 网络数据相匹配,只需打印所有数据与时间戳一起输出,并进行后处理,通过时间戳将 Wifi 网络与 GPS 坐标进行匹配。

I think you should be more specific in your goal.

If all you want to do is get GPS coords when a Wifi network is sniffed, just do (psuedo-code):

while True:
    if networkSniffed():
        async_GetGPSCoords()

If you want a log of all GPS coords and want to match that up with Wifi network data, just print all the data out along with timestamps, and do post-processing do match up Wifi networks with GPS coords via timestamp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文