def getRecServerSocket(port): # This function creates a socket to block server and returns it # Make socket transport = TSocket.TSocket('localhost', port) transport.setTimeout(30000) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Recommender.Client(protocol) # Connect! #print "Connecting to block server on port", port try: transport.open() except Exception as e: print e log.error( "Exception while connecting to block server, check if server is running on port: {}" .format(port)) transport.close() exit(1) return client