How do I filter incoming data from a socket in Python?
Question: I am using a UDP socket program for one of my projects to read in incoming
data from an EEG headset. I then use this data to control servo motors in a
robotic arm. The code that I am using to create the socket and print out the
data works fine.

    
    
    import socket
    import subprocess
    
    UDP_IP = "169.254.110.133"
    UDP_PORT = 50000
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind((UDP_IP, UDP_PORT))
    
    while True:
            (data,addr) = sock.recvfrom(1024)
            print(data)
    

The data that is coming in is raw EEG data from the headset and it looks like
this in the terminal:

    
    
    SourceTime 31972
    
    TargetCode 1
    
    ResultCode 0
    
    Feedback 1
    
    PauseApplication 0
    
    CursorPosX 717
    
    CursorPosY 2047
    
    CursorPosZ 2047
    

This data has 33 different sections each few milliseconds. The data always
comes in the same order. For my code, I only need a few pieces of information
from the data (TargetCode, ResultCode, and Feedback). How do I filter out
these sections and use them in my code. Is there a way to look for TargetCode
and copy the number? Or since TargetCode is always number 24 in the stream of
33 sections can I extract it that way?


Answer: This might help.

**Server**

    
    
    import socket              
    
    s = socket.socket()         
    UDP_IP = "169.254.110.133"
    UDP_PORT = 50000              
    sock.bind((UDP_IP, UDP_PORT))        
    
    s.listen(5)                 
    while True:
       c, addr = s.accept()     
       c.send('Connected to server.')
       c.close()           
    

**Client**

    
    
    import socket               
    UDP_IP = "169.254.110.133"
    UDP_PORT = 50000   
    s = socket.socket()         
    s.connect((UDP_IP,UDP_PORT))
    print (s.recv(1024))
    s.close                     
    

