def run(self): """@param utc: unit test class""" sleep(0.1) sck = socket(AF_INET, SOCK_STREAM) sck.connect(('127.0.0.1', TESTING_PORT)) sck = Socket(sck) sck.write('Hello World') self.pkdata = sck.read(3, less=False, peek=True) self.data = sck.read(3) self.utc.assertRaises(ChannelClosed, sck.read, 1) sck.close()
def run(self): """@param utc: unit test class""" sleep(0.1) sck = socket(AF_INET, SOCK_STREAM) sck.connect(('127.0.0.1', TESTING_PORT)) sck = Socket(sck) self.utc.assertEquals(sck.blocking, True) self.utc.assertEquals(sck.read(1), 'L') pkdata = sck.read(100, less=True, peek=True) data = sck.read(100, less=True) self.utc.assertEquals(pkdata, 'ong string? Not enough.') self.utc.assertEquals(data, 'ong string? Not enough.') self.utc.assertRaises(ChannelClosed, sck.read, 1) sck.close()
def run(self): sleep(0.2) sck = Socket(socket(AF_INET, SOCK_STREAM)) sck.connect(('127.0.0.1', TESTING_PORT)) sck.write('Hello World') self.utc.assertEquals(sck.read(1), '1') self.utc.assertEquals(sck.read(1), '2') sck.close()
def run(self): sck = socket(AF_INET, SOCK_STREAM) sck.connect(('127.0.0.1', TESTING_PORT)) sck = Socket(sck) sck.settimeout(5) self.utc.assertEquals(sck.read(2), 'Ye') self.utc.assertEquals(sck.read(1), 's') sck.write('Hello World') sck.close()
def read(self, peek=False): """ Attempts to read a JSON frame. Throws DataNotAvailable on no data. If blocking and null-frame received will return None """ if self.blocking: try: frame = self.frames.popleft() return frame except IndexError: pass # we simply need to catch a new frame # so no cached frames for us. Let's fetch one directly.. b_fln = Socket.read(self, 4) i_fln, = struct.unpack('>L', str(b_fln)) if i_fln == 0: return None pdata = Socket.read(self, i_fln) try: self.frames.append(json.loads(str(pdata))) except ValueError: # invalid JSON self.close() raise FatalException, 'Invalid JSON received' return self.frames.popleft() else: try: frame = self.frames.popleft() except IndexError: raise DataNotAvailable if peek: self.frames.appendleft(frame) return frame
def read(self, peek=False): """ Attempts to read a JSON frame. Throws DataNotAvailable on no data. If blocking and null-frame received will return None """ if self.blocking: try: frame = self.frames.popleft() return frame except IndexError: pass # we simply need to catch a frame # so no cached frames for us. Let's fetch one directly.. b_fln = Socket.read(self, 4) i_fln, = struct.unpack('>L', str(b_fln)) if i_fln == 0: return None pdata = Socket.read(self, i_fln) try: self.frames.append(json.loads(str(pdata))) except ValueError: # invalid JSON self.close() raise FatalException, 'Invalid JSON received' return self.frames.popleft() else: try: frame = self.frames.popleft() except IndexError: raise DataNotAvailable if peek: self.frames.appendleft(frame) return frame
def on_readable(self): """Read JSON frames Empty frames are None. """ Socket.on_readable(self) self.last_received_data = time() while True: try: # peek the frame size (keep in mind we need to remove # if from the buffer later, it's only a peek!) b_fln = Socket.read(self, 4, peek=True) # this throws DataNotAvailable - we'll catch it later # this throws FatalException - let it propagate i_fln, = struct.unpack('>L', str(b_fln)) if i_fln == 0: # special case - frame size is zero. Consume it # and attempt another frame Socket.read(self, 4) continue # Now read the frame (alongside with it's size) pdata = Socket.read(self, i_fln+4) # this throws DataNotAvailable - we'll catch it later # this throws FatalException - let it propagate try: self.frames.append(json.loads(str(pdata[4:]))) except ValueError: # invalid JSON self.close() raise FatalException, 'Invalid JSON received' except DataNotAvailable: # all frames readed, good-bye break
def on_readable(self): """Read JSON frames Empty frames are None. """ Socket.on_readable(self) self.last_received_data = time() while True: try: # peek the frame size (keep in mind we need to remove # if from the buffer later, it's only a peek!) b_fln = Socket.read(self, 4, peek=True) # this throws DataNotAvailable - we'll catch it later # this throws FatalException - let it propagate i_fln, = struct.unpack('>L', str(b_fln)) if i_fln == 0: # special case - frame size is zero. Consume it # and attempt another frame Socket.read(self, 4) continue # Now read the frame (alongside with it's size) pdata = Socket.read(self, i_fln+4) # this throws DataNotAvailable - we'll catch it later # this throws FatalException - let it propagate try: self.frames.append(json.loads(str(pdata[4:]))) except ValueError: # invalid JSON self.close() raise FatalException, 'Invalid JSON received' except DataNotAvailable: # all frames readed, good-bye break