def test_webhook_skips_closed_websockets(self): fake_websocket = mock.MagicMock() fake_websocket.closed = True server.receive(fake_websocket) response = self.app.post( '/webhook', data=self.json, content_type='application/json' ) fake_websocket.send.assert_not_called()
def test_webhook_notifies_open_websockets(self): fake_websocket = mock.MagicMock() fake_websocket.closed = False server.receive(fake_websocket) response = self.app.post( '/webhook', data=self.json, content_type='application/json' ) lat_lon = json.dumps({'lat': 40.9126, 'lon': -73.8371}) fake_websocket.send.assert_called_with(lat_lon)
def listenResponse(port): serverSocket = create(host, port) while 1: data = receive(serverSocket) dataJSON = json.loads(data) print bcolors.Green + 'Hey, i received the data that i have subscribed from {} {}: {}'.format( dataJSON['name'], dataJSON['id'], dataJSON['data']) + bcolors.ENDC
def test_receive_route_adds_socket_to_observers(self): fake_websocket = mock.MagicMock() server.receive(fake_websocket) self.assertEqual(1, len(server.WEBSOCKETS))
newData['name'] = 'intermed' newData['id'] = '3' newJSON = json.dumps(newData) sendmessage(host, i2port, newJSON) intermedList = list() if args.intermed[0] == 1: interId = 1 print 'initializing i1' ServerSocket = create(host, i1port) elif args.intermed[0] == 2: interId = 2 print 'initializing i2' ServerSocket = create(host, i2port) elif args.intermed[0] == 3: interId = 3 print 'initializing i3' ServerSocket = create(host, i3port) while 1: data = receive(ServerSocket) dataJSON = json.loads(data) if dataJSON['name'] == 'publisher': print bcolors.Blue + 'Receiving from {} {} the data {}'.format(dataJSON['name'], dataJSON['id'], dataJSON['data']) + bcolors.ENDC seeOnList(dataJSON) elif dataJSON['name'] == 'intermed': print bcolors.Blue + 'Receiving from {} {} the data {}'.format(dataJSON['name'], dataJSON['id'], dataJSON['data']) + bcolors.ENDC spreadTheWord(dataJSON) elif dataJSON['name'] == 'subscriber': print bcolors.Blue + 'Receiving from {} {} the data {}'.format(dataJSON['name'], dataJSON['id'], dataJSON['data']) + bcolors.ENDC expressIntention(dataJSON)
tps = TPSCounter(servo_control) tps.start() print("### Initialization finished in {:1.4f} seconds".format( (time.time() - start_time))) print("##########################################################\n\n") while not server.termination_pending: print("\tWaiting for incoming connection...") led.ChangeDutyCycle(0.5) # wait until a connection is established server.await_connection() while server.connection_active: led.ChangeDutyCycle(0) data_type = server.receive() if len(data_type) == 1 and data_type == server.SERVER_REQUEST: server.send_byte(server.SERVER_READY) # receive 3-byte data frame data = server.receive() if len(data) == 3: servo_control.set_values(data[0], data[1], data[2]) tps.inc() else: continue # if last bit of byte 3 is set cancel connection
s.close() print('Stopping the client') exit(code) if __name__ == "__main__": client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while True: try: client_socket.connect((socket.gethostname(), PORT)) break except (socket.error, OverflowError): pass message = {} try: message = receive(client_socket) except (ConnectionAbortedError, ConnectionResetError): exit_client(1, client_socket) username = message['username'] print(f'You entered the game under the name {username}') if message['action'] == 'continue': print('Game continued from the last save') elif message['action'] == 'begin': print('A new game is started') else: exit_client(1, client_socket) answer = '' while True: try: message = receive(client_socket) except (ConnectionAbortedError, ConnectionResetError):
callmethod = "python" print("Server Name/IP: '" + server.socket.gethostname() + "'") print("On port: '" + str(port) + "'") print() try: while True: print("Waiting for a connection...") connection = server.acceptConnections(serv) try: print() while True: get = server.receive(connection) result = "" if get != "": print("> Received '" + get + "'") if get[:4] == "close": print("Client attempted to close server") server.send(connection, "Use: close [password]", False) if get == "close" + str(pwd): break exit() if get: if get == "SETCALLMETHOD<C++>": if getCodeDisallowement(): print("> Denied switch to C++ callmethod") result = "DENIED" else: