def start(self): """ Här startas klienten och en tråd skapas som lyssnar för inkommande paket från servern. """ try: try: self.server.connect((self.ip, self.port)) self.running = True except ConnectionRefusedError: print("Can not connect to host!") return print("Connected!") while self.running: # maintains a list of possible input streams sockets_list = [self.server] read_sockets, write_socket, error_socket = select.select(sockets_list, [], []) for socks in read_sockets: if socks == self.server: packet = socks.recv(2048) if (packet is None): continue self.receivedPacket(packet.decode()) except ConnectionResetError: print("Connection lost to server!") self.stop()
def parse(self, bytes_): LOG.debug("Parsing %s bytes %s", len(bytes_), fmt_bytes(bytes_)) maybe_parsed = packet.decode(bytes_) if maybe_parsed: return {'uid': self.uid, 'formatted': True, 'fields': maybe_parsed} return {'uid': self.uid, 'formatted': False}
def receiveUdpPackage(self, port): receiving = True while receiving: network_data = self.my_socket.recv(2048) encoded = network_data.encode('hex') result = packet.decode(encoded, port) if result != -1: receiving = False return ("OK", result)
def recv(self): while True: raw_data, addr = self._socket.recvfrom(_RECV_BUFFER_SIZE) if raw_data == None or len(raw_data) == 0: raise IOError('disconnected') try: return decode(raw_data), addr except Exception as e: print 'Invalid packet from', self._net_addr, '-', e
def send(self, packet): """ Sends a given packet to the server and waits for a reply. @param packet: Packet to be sent. @type packet: Packet instance. @return: Packet return by the server.. @rtype: Packet instance or None. """ if not isinstance(packet, Packet): return None tmp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tmp_socket.connect((self.target_address, self.target_port)) tmp_socket.send(packet.encode()) data = tmp_socket.recv(MAX_RESPONSE_SIZE) tmp_socket.close() return decode(data, self.list_manager)
def read_input(fileName): maxDataSize = 2 file = open(fileName, "rb") contents = file.read() numPackets = math.ceil(len(contents) / maxDataSize) index = 0 packets = [] for i in range(numPackets): content = [] for j in range(index, min(index + maxDataSize, len(contents))): content.append(contents[j]) index += maxDataSize packet = bytes(content) packets.append(packet.decode("utf-8")) return packets
def packetAnalyzer(pkt): # sending decoded packet info to detection bridge res = packet.decode(pkt) if res != None: sio.emit('detect', packet.decode(pkt))
def packetAnalyzer(pkt): # sending decoded packet info to detection bridge sio.emit('detect', packet.decode(pkt))