开发者

Two simultaneous Python loops with one result

开发者 https://www.devze.com 2023-04-08 05:35 出处:网络
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 wh

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.sl开发者_C百科eep(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.


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))  


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)  


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号