def handle(self, *args, **options): # Get node ID node_id = int(args[0]) # Create socket client_socket = socket(AF_INET, SOCK_STREAM) # Connect to host on specified port client_socket.connect(('127.0.0.1', server_port())) # Try to contact server try: sendMsg(client_socket, str(node_id)) except: abnormalShutdown(client_socket, "Error") # Shut down write side of socket client_socket.shutdown(1) # Receive incoming message from server try: response = recvMsg(client_socket) except: print "Could not receive message from server" # Close socket client_socket.close() return response
def get_node_status(node_id): # Create socket clientSocket = socket(AF_INET, SOCK_STREAM) # Connect to localhost on specified port clientSocket.connect(('127.0.0.1', server_port())) # Print connection information to console #print("Connected to " + str(clientSocket.getpeername()[0]) + " on port " + str(server_port())) try: sendMsg(clientSocket, str(node_id)) except: abnormalShutdown(clientSocket, "Error") # Shut down write side of socket clientSocket.shutdown(1) # Receive incoming message from server return recvMsg(clientSocket) #print("Received from server " + str(incomingMsg)) # Close socket clientSocket.close()
def main(): configs = utils.loadConfigs() # create and initialize analysis object analyzer = analysis.Analysis("USDT_BTC", 300, 500, configs) analyzer.setup() # create and initialize transactor (intially holding btc) trans_que = queue.Queue() trans = transactor.TransactorThread("USDT_BTC", trans_que, True, configs) trans.start() utils.sendMsg("trader started") utils.write("trader started") while True: if (analyzer.update()): val = analyzer.analyze() trans_que.put(val) time.sleep(60) trans.join()
def run(self): """ Initiated with start()""" while not self.stoprequest.isSet(): try: action = self.trans_q.get(True) # utils.write("obtained value") if (action == 1 and not self.holding): utils.write("buying") utils.sendMsg("buying") ret_val = self.buy() utils.write(ret_val) utils.sendMsg(parseTradeReturn(ret_val)) elif (action == -1 and self.holding): utils.write("selling") utils.sendMsg("selling") ret_val = self.sell() utils.write(ret_val) utils.sendMsg(parseTradeReturn(ret_val)) except queue.Empty: continue
ser = serial.Serial(settings.TTY_PORT, '9600', timeout=5) time.sleep(2) try: ser.write(str(incomingMsg)) except: print("Error during serial write") time.sleep(2) try: result = ser.read() except: print("Error during serial read") # Send serial value to client sendMsg(conn, result) # Try to shut down write side of socket # If successful, print server-side messages # Otherwise inform server that client abnormally terminated try: # Shutdown write side of socket conn.shutdown(1) except: print("Abnormal termination from client\n") # Clock the connection conn.close()