def showTree(request): return {"tutorial": "Little Dummy"} returnValue="" #a = MessageClientSend(MessageAttr.listServers,{"yay":"yay"}) a = MessageClientSend("dotgraph",{"yay":"yay"}) #create an INET, STREAMing socket s = socket.socket( socket.AF_INET, socket.SOCK_STREAM) #now connect to the web server on port 80 # - the normal http port print "trying to connect" #print a.toxml() s.connect(("127.0.0.1", PORT)) s.send(a.toxml()) data = s.recv (1024) if data: messageGen = Message() reply = messageGen.parse(data) reply.getCommand() returnValue=html.fromstring(reply.getDataDict()["Dot"][0]).text s.close() return Response(returnValue)
def getDataFromServer(command,paramsDict={},noReturn=False): ''' Send a command to the Ockle server, and return the responce dict :param command: The command to send :param paramsDict: the dictionary that is sent with command arguments :param noReturn: Should we not expect a reply. Used in cases were we want to restart the Ockle server :return: A dict with the response data, None if we failed to connect ''' returnValue="" def recv(sendMessage,noReturn): ''' Loop to receive a message''' s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(SOCKET_TIMEOUT) n = '' data = '' try: s.connect((OCKLE_SERVER_HOSTNAME, PORT)) s.sendall(str(len(sendMessage)) +":" +sendMessage) if noReturn: s.close() return None,None else: while 1: c = s.recv(1) if c == ':': break n += c n = int(n) while n > 0: data += s.recv(n) n -= len(data) s.close() print data return None, data except socket.error as e: s.close() return e, None a = MessageClientSend(command,paramsDict) m, data = recv(a.toxml(),noReturn) #print 'recv: ', 'error=', m, 'data=', data if data: messageGen = Message() print "got the following data:" print data reply = messageGen.parse(data) returnValue= reply.getDataDict() if type(returnValue) == dict: returnValue = trimOneObjectListsFromDict(returnValue) return returnValue