Changeset 711

Show
Ignore:
Timestamp:
11/04/07 18:09:10 (13 months ago)
Author:
michiel
Message:

add documentation to the socket handling so I can remember when/why/how it's done

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tools/nsa/nsa.py

    r710 r711  
    149149                PORT = 8764               # Arbitrary non-privileged port 
    150150                if sock == None: 
     151                        # no socket yet, so lets create one 
    151152                        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    152153                else: 
    153154                        self.sock = sock 
    154155 
     156                # Allow reuse of address 
    155157                self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
     158                # Bind to specified host+port 
    156159                self.sock.bind((HOST, PORT)) 
     160                # listen with backlog of 1 
    157161                self.sock.listen(1) 
    158162         
     
    161165                done = False 
    162166                if self.sock: 
     167                        # test for something on the port. 
     168                        # the timeout can be set to something, this is just a non-blocking socket now 
    163169                        (r, w, e) = select.select([self.sock], [], [], 0) 
    164170                        if r: 
     171                                # we have data so let's accept it 
    165172                                (client, addr) = self.sock.accept() 
    166173                                while not done: 
    167174                                        data = None 
    168175                                        try: 
     176                                                # Here, we wait until the client starts sending data. 
     177                                                # if we don't wait (in a blocking fashion) here, we'll 
     178                                                # get NOTREADY errors (we take care of those below with 
     179                                                # the if e.args[0] == 11 just in case). 
    169180                                                data = client.recv(1024) 
    170181                                        except socket.error, e: 
     182                                                # Socket not ready yet (err 11). Just try again. 
    171183                                                if e.args[0] == 11: 
    172184                                                        pass 
    173185                                                else: 
    174186                                                        raise e 
     187                                        # We recieved data? (no exception on recv?) 
    175188                                        if data: 
     189                                                # Append 
    176190                                                alldata += data 
     191                                                # We recieve 1024 max on the socket each recv, so 
     192                                                # if the len of data is less than 1024, there could 
     193                                                # possibly be more left. If not, we're done reading. 
    177194                                                if len(data) < 1024: 
    178195                                                        done = True 
     196                                # Close the connection so the client can terminate 
    179197                                client.close() 
     198                                # Send the data to the channel 
    180199                                bot.say_it(alldata) 
    181200