Python Socket

Details to come.
Here’s a short description in the meanwhile:

This is a class I wrote for a host, which should handle some requests, given by clients.
The part to notice here is the way select() is used. I found that socket.recv() was a blocking call and since any method of trying to make it non-blocking failed with some error, I had to use select(). Select reads from selectable inputs (ie. socket or user inputs), and returns a list of sockets with data read. You may then use socket.recv() on these sockets, because there is data avaiable and this won’t block the program while waiting for data.

class ClientHandler():
    # Initializes a client handler
    # Takes the host socket as argument
    def __init__(self, socket):
        self.hostsoc = socket
        self.sockets = [self.hostsoc]       
        self.clients = []                 

    # Automatically removes dead clients
    def serveclients(self):

        # Select available sockets
        rready, wready, err = select.select(self.sockets, [], [], 0)
        for s in rready:

            # If there was a new connection
            if s == self.hostsoc:                                                                                                                                                                                                           
                # Accept the new socket
                c, addr = s.accept()
                
                # Create new clients of the new socket
                client = Client(c, addr) 
                self.clients.append(client)

                # Append the new socket to the client handlers list of sockets                
                self.sockets.append(c)

            # If data was available on existing clients                                                                                                                      
            else:
                #Read                                                                                                                                                                                                              
                data = s.recv(1024)

                if data:
                    # Get the sender (client)
                    for c in self.clients:
                        if s == c.socket():
                            sender = c
                            break

                    # Append the data to the clients queue
                    sender.appenddata(data)

                    # Handle any complete requests there may be
                    sender.handle()                    

                    # Add data to client data queue
                    #print "Received %s" % ( data )

                else:
                    
                    # Mark the corresponding client as inactive ...
                    for c in self.clients:
                        if s == c.socket():
                            c.deactivate()
                            break                        

                    # ... and remove the socket descriptor from the list of avilable sockets                                                                      
                    self.sockets.remove(s)

                    # Close the socket
                    s.close()                                                        
        # Return            
        return True

 

Timing in Python: Regarding time.clock()

I recently had to use a timer in a Python-program, and happened to run into a problem with the time.clock() function, which is supposed to be the most accurate [more accurate than time.time()]. The problem was, I found not the accuracy, but rather that clock() seemed to function poorly, when some blocing code, ie. socket.recv() and socket.send() was involved.

The problem:

import time
 tstart = time.clock()
 elapsed = time.clock() - tstart

The solution:

from datetime import datetimetstart = datetime.now()
 elapsed = datetime.now() - start
 elapsed.seconds

Worked just perfectly.

Eternity 2

I’ve been writing a solver for the Eternity 2 puzzle. Here’s a sample, when setting the dimensions to 4×4. The puzzle itself is 16×16.

It’s written in Python, which of course is not the optimal language, but performance wasn’t the goal anyway.

The goal, amongst others, was to explore techniques for solving the puzzle, and to get more into Python.

More to come on this…


ET2Mini16

 

ET2_Triangle

The solver now, partially at least, works in a distributed environment, using sockets for communication. Some features that might be handy, if the solver should ever find a solution are missing, but that was never the point of this project anyway. More to come on the distributed solver as well.

I’ve built a small cluster of Raspberry Pi’s for testing:

PiCluster1