Ejemplo n.º 1
0
Archivo: net.py Proyecto: korc/krutils
		if nodata_delay is not None:
			while self.sock in select.select([self.sock],[],[],nodata_delay)[0]:
				data=self.sock.recv(size)
				if data=='': break
				buf.append(data)
		return ''.join(buf)
	def recv(self,size=None,nodata_delay=None):
		data=self.sock_recv(size,nodata_delay)
		if data=='': raise EndOfDataException,"No more data"
		if self.verbose: print 'recv(%d): %r'%(len(data),data)
		return data
	def close(self):
		try: self.sock.shutdown(2)
		except socket.error: pass
		self.sock.close()
proputil.gen_props(TcpSock)

class SSLSock(TcpSock):
	def __init__(self,*args,**kwargs):
		TcpSock.__init__(self,*args,**kwargs)
		self.raw_sock=self.sock
		self.sock=Connection(Context(TLSv1_METHOD),self.raw_sock)
		self.sock.set_connect_state()
		self.sock.do_handshake()
	def sock_recv(self,size,nodata_delay):
		if size is None: size=self.recv_size
		try: return self.sock.read(size)
		except WantReadError:
			tmout=self.timeout
			if tmout is None: raise
			if not select.select([self.sock],[],[],tmout)[0]:
Ejemplo n.º 2
0
				except NoMatch: pass
			try:
				data=self.data_read()
				self.data_log.append(data)
				if self.debug: print "data_read: %r"%(data)
				if data=='': raise EndOfData,"no more data"
			except Exception,e:
				others=OnNever()
				if self.debug and not isinstance(e,EndOfData):
					print "Got exception %r while having %r, going to %r"%(e,data,condlist)
				try: match=self.find_match(e,map(lambda x: x if isinstance(x,OnException) else others,condlist))
				except NoMatch: raise e
				return match
			self.data_buffer.append(data)
	def data_read(self): raise NotImplementedError,"data_read() needs to be implemented"
proputil.gen_props(ReaderBase)

class StreamReader(ReaderBase):
	_init_args=('stream',)
	def data_read(self): return self.stream.read()

class FDReader(ReaderBase):
	_init_args=('fd',)
	def data_read(self):
		return os.read(self.fd,8192)

class StateMachine(DynInit):
	_default_attrs=dict(states={'start':(["end"],),'end':(None,OnException(EndOfData))},state="start",next_states=None,prev_next_states=None,debug=False,log=[])
	_init_args=('reader',)
	def execute_handlers(self,state):
		try: handler=getattr(self,"on_%s"%(state))