def getPackets_func(request, environ, sessionid): retData = {} try: # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TFramedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = PacketStreamer.Client(protocol) # Connect! transport.open() packets = client.getPackets(sessionid) # Close! transport.close() retData = { 'status': '200 OK', 'body': json.dumps(packets), 'headers': [('Content-type', 'application/json')], # list of tuples } except Thrift.TException, tx: retData = { 'status': '500 Internal Server Error', 'body': 'error: %s' % tx.message, 'headers': [('Content-type', 'application/json')], # list of tuples }
logging.Formatter("%(name)s: %(levelname)s %(message)s")) self.log.addHandler(handler) def ping(self): self.log.debug('ping()') return true def pushPacketSync(self, packet): self.log.debug('receive a packet synchronously: %s' % (packet)) return 0 def pushPacketAsync(self, packet): self.log.debug('receive a packet Asynchronously: %s' % (packet)) handler = PacketStreamerHandler() processor = PacketStreamer.Processor(handler) transport = TSocket.TServerSocket(9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) # You could do one of these for a multithreaded server #server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) #server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) print 'Starting the server...' server.serve() print 'done.'
from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol try: # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TFramedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = PacketStreamer.Client(protocol) # Connect! transport.open() while 1: packets = client.getPackets("session1") print 'session1 packets num: %d' % (len(packets)) count = 1 for packet in packets: print "Packet %d: %s"% (count, packet) if "FilterTimeout" in packet: sys.exit() count += 1 # Close!