Ejemplo n.º 1
0
    def _process_attacks_queue(self) -> None:
        conn = Connection(config.get_broker_url())
        with conn.channel() as channel:
            producer = Producer(channel)
            by_label = defaultdict(list)
            while not self._q.empty():
                try:
                    ar: models.AttackResult = self._q.get_nowait()
                except Empty:
                    continue
                else:
                    by_label[ar.get_label_key()].append(ar)

            for ar_list in by_label.values():
                if not ar_list:
                    continue

                monitor_message = {
                    'type': 'flag_submit',
                    'data': ar_list[0].get_label_values(),
                    'value': len(ar_list),
                }

                producer.publish(
                    monitor_message,
                    exchange='',
                    routing_key='forcad-monitoring',
                )
Ejemplo n.º 2
0
    def _send_dhcp_notification(self, context, data, methodname):
        #self._dhcp_agent_notifier.notify(context, data, methodname)
        task_exchange = Exchange('nspagent', type='topic')   
	create_result = {"oslo.message" :{ 
			  #'message_id': '5c329c20-d435-444c-8fa2-e1b54592219c',
			  #'publisher_id': 'compute.host1',
			  'method': 'network_create_end',
			  #'_context_user_id': None,
		 	  #'_context_project_id': None,
			  #'_context_is_admin': True,
			  "args" : {"payload": {"network": network
					#{"id": "24783e1a-63fe-43d5-9989-e1515c24eecd"}
				      }		
				   }
		         },
			"oslo.version":'2.0',
		      }
	create_result["oslo.message" ] = json.dumps(create_result["oslo.message"])
	connection = Connection('amqp://*****:*****@192.168.49.22:5672//')  
	channel = connection.channel()  
   
	message=Message(channel,body= 'subenet_create_end')  
   
	# produce  
	producer = Producer(channel,serializer='json') 
        print message.body, task_exchange 
	producer.publish(create_result, routing_key='dhcp_agent')  
Ejemplo n.º 3
0
class QueuePush(object):
    def __init__(self,Queue_Server,Queue_Port,Queue_User,Queue_PassWord,Queue_Path):
        self.usr=Queue_User
        self.psw=Queue_PassWord
        self.server=Queue_Server
        self.port=Queue_Port
        self.path=Queue_Path
        self.connection=None
        self.smsExchange=None
    def _InitConnect(self):
        if self.connection is None or self.connection.connected==False:
            self.connection = Connection(hostname=self.server,port=self.port,userid=self.usr,password=self.psw,virtual_host=self.path)
            self.channel = self.connection.channel()
            self.producer=Producer(self.channel)
            self.smsExchange=Exchange("sys.sms",type='topic',channel=self.channel,durable=True,delivery_mode=2)
            self.smsCodeProduce=Producer(self.channel,self.smsExchange,routing_key='sms.code')
    def Push(self,queueid,connectid,body):
        self.rawPush(queueid,{'connid':connectid},body)
    def Close(self,queueid,connectid):
        self.rawPush(queueid,{'connid':connectid,'close_connect':'1'},'close')
    def rawPush(self,routing_key,headers,body):
        self._InitConnect()
        self.producer.publish(body=body,delivery_mode=2,headers=headers,
                              routing_key=routing_key,retry=True,compression='gzip')
    def sendCode(self,phone,code):
        self._InitConnect()
        json_str=json.dumps({'phone':str(phone),"content":u"您的莱信验证码为:%s,请在5分钟内输入完成验证。【莱福思】"%str(code)},ensure_ascii=False)
        self.smsCodeProduce.publish(body=json_str,retry=True,compression='gzip')
Ejemplo n.º 4
0
class UdpService(os_service.Service):
    """Listener for the udpservice service."""
    def start(self):
        """Bind the UDP socket and handle incoming data."""
        # ensure dispatcher is configured before starting other services
        super(UdpService, self).start()
         
        if cfg.CONF.udpservice.udp_address:
            self.tg.add_thread(self.start_udp)

    def convert_sample_to_event_data(self,msg):
        event_data = {'event_type': 'infra','message_id':six.text_type(uuid.uuid4()),'publisher_id': 'cpe_publisher_id','timestamp':datetime.datetime.now().isoformat(),'priority':'INFO','payload':msg}
        return event_data
    
    def setup_rabbit_mq_channel(self):
        service_exchange = Exchange(cfg.CONF.udpservice.acord_control_exchange, "topic", durable=False)
        rabbit_host = cfg.CONF.udpservice.rabbit_hosts
        rabbit_user = cfg.CONF.udpservice.rabbit_userid 
        rabbit_password = cfg.CONF.udpservice.rabbit_password
        # connections/channels
        connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password)
        print 'Connection to RabbitMQ server successful'
        channel = connection.channel()
        # produce
        self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')

    def start_udp(self):
        address_family = socket.AF_INET
        if netutils.is_valid_ipv6(cfg.CONF.udpservice.udp_address):
            address_family = socket.AF_INET6
        udp = socket.socket(address_family, socket.SOCK_DGRAM)
        udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        udp.bind((cfg.CONF.udpservice.udp_address,
                  cfg.CONF.udpservice.udp_port))

        self.setup_rabbit_mq_channel()
        self.udp_run = True
        while self.udp_run:
            # NOTE(jd) Arbitrary limit of 64K because that ought to be
            # enough for anybody.
            data, source = udp.recvfrom(64 * units.Ki)
            try:
                sample = msgpack.loads(data, encoding='utf-8')
            except Exception:
                LOG.warn(_("UDP: Cannot decode data sent by %s"), source)
            else:
                try:
                    if sample.has_key("event_type"):
                         LOG.debug(_("recevied event  :%s"),sample)
                         self.producer.publish(sample)
                    else:
                         LOG.debug(_("recevied Sample  :%s"),sample)
                         msg = self.convert_sample_to_event_data(sample)
                         self.producer.publish(msg)
                except Exception:
                    LOG.exception(_("UDP: Unable to store meter"))

    def stop(self):
        self.udp_run = False
        super(UdpService, self).stop()
Ejemplo n.º 5
0
    def test_publish__consume(self):
        connection = BrokerConnection(transport=Transport)
        channel = connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        consumer = Consumer(channel, self.queue)

        producer.publish({"hello2": "world2"})
        _received = []

        def callback(message_data, message):
            _received.append(message_data)
            message.ack()

        consumer.register_callback(callback)
        consumer.consume()

        self.assertIn(channel, channel.connection.cycle._channels)
        try:
            connection.drain_events(timeout=1)
            self.assertTrue(_received)
            self.assertRaises(socket.timeout,
                              connection.drain_events,
                              timeout=0.01)
        finally:
            channel.close()
Ejemplo n.º 6
0
    def test_publish__consume(self):
        connection = BrokerConnection(transport=Transport)
        channel = connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        consumer = Consumer(channel, self.queue)

        producer.publish({"hello2": "world2"})
        _received = []

        def callback(message_data, message):
            _received.append(message_data)
            message.ack()

        consumer.register_callback(callback)
        consumer.consume()

        self.assertTrue(channel._poller._can_start())
        try:
            connection.drain_events(timeout=1)
            self.assertTrue(_received)
            self.assertFalse(channel._poller._can_start())
            self.assertRaises(socket.timeout,
                              connection.drain_events, timeout=0.01)
        finally:
            channel.close()
Ejemplo n.º 7
0
class QueuePush(object):
    def __init__(self, Queue_Server, Queue_Port, Queue_User, Queue_PassWord,
                 Queue_Path):
        self.usr = Queue_User
        self.psw = Queue_PassWord
        self.server = Queue_Server
        self.port = Queue_Port
        self.path = Queue_Path
        self.connection = None
        self.smsExchange = None

    def _InitConnect(self):
        if self.connection is None or self.connection.connected == False:
            self.connection = Connection(hostname=self.server,
                                         port=self.port,
                                         userid=self.usr,
                                         password=self.psw,
                                         virtual_host=self.path)
            self.channel = self.connection.channel()
            self.producer = Producer(self.channel)
            self.smsExchange = Exchange("sys.sms",
                                        type='topic',
                                        channel=self.channel,
                                        durable=True,
                                        delivery_mode=2)
            self.smsCodeProduce = Producer(self.channel,
                                           self.smsExchange,
                                           routing_key='sms.code')

    def Push(self, queueid, connectid, body):
        self.rawPush(queueid, {'connid': connectid}, body)

    def Close(self, queueid, connectid):
        self.rawPush(queueid, {
            'connid': connectid,
            'close_connect': '1'
        }, 'close')

    def rawPush(self, routing_key, headers, body):
        self._InitConnect()
        self.producer.publish(body=body,
                              delivery_mode=2,
                              headers=headers,
                              routing_key=routing_key,
                              retry=True,
                              compression='gzip')

    def sendCode(self, phone, code):
        self._InitConnect()
        json_str = json.dumps(
            {
                'phone': str(phone),
                "content": u"您的莱信验证码为:%s,请在5分钟内输入完成验证。【莱福思】" % str(code)
            },
            ensure_ascii=False)
        self.smsCodeProduce.publish(body=json_str,
                                    retry=True,
                                    compression='gzip')
Ejemplo n.º 8
0
    def test_produce_consume(self):
        channel = self.c.channel()
        producer = Producer(channel, self.e)
        consumer1 = Consumer(channel, self.q)
        consumer2 = Consumer(channel, self.q2)
        self.q2(channel).declare()

        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory2")

        _received1 = []
        _received2 = []

        def callback1(message_data, message):
            _received1.append(message)
            message.ack()

        def callback2(message_data, message):
            _received2.append(message)
            message.ack()

        consumer1.register_callback(callback1)
        consumer2.register_callback(callback2)

        consumer1.consume()
        consumer2.consume()

        while 1:
            if len(_received1) + len(_received2) == 20:
                break
            self.c.drain_events()

        self.assertEqual(len(_received1) + len(_received2), 20)

        # compression
        producer.publish({"compressed": True},
                         routing_key="test_transport_memory",
                         compression="zlib")
        m = self.q(channel).get()
        self.assertDictEqual(m.payload, {"compressed": True})

        # queue.delete
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")
        self.assertTrue(self.q(channel).get())
        self.q(channel).delete()
        self.q(channel).declare()
        self.assertIsNone(self.q(channel).get())

        # queue.purge
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory2")
        self.assertTrue(self.q2(channel).get())
        self.q2(channel).purge()
        self.assertIsNone(self.q2(channel).get())
Ejemplo n.º 9
0
def main():

    connection = Connection('amqp://*****:*****@localhost:5672//')
    _channel = connection.channel()
    _exchange = Exchange('neutron', type='topic')

    pro = Producer(channel=_channel,
                   exchange=_exchange,
                   routing_key='q-plugin')
    pro.publish(MSG)
 def publish(self, data, timeout=None):
     p = Producer(channel=self.channel,
                  exchange=Exchange(self.target.exchange_name, type=self.EXCHANGE_TYPE,
                                    durable=self.options['durable'],
                                    auto_delete=self.options['auto_delete']),
                  routing_key=self.target.routing_key)
     if timeout:
         p.publish(data, headers={'ttl': (timeout * 1000)})
     else:
         p.publish(data)
Ejemplo n.º 11
0
 def _publish(self, type, arguments, destination=None, reply_ticket=None, channel=None):
     message = {"method": type, "arguments": arguments, "destination": destination}
     if reply_ticket:
         message["reply_to"] = {"exchange": self.reply_exchange.name, "routing_key": reply_ticket}
     chan = channel or self.connection.channel()
     producer = Producer(chan, exchange=self.exchange)
     try:
         producer.publish(message)
     finally:
         channel or chan.close()
Ejemplo n.º 12
0
 def _publish_reply(self, reply, exchange, routing_key, channel=None):
     chan = channel or self.connection.channel()
     try:
         exchange = Exchange(
             exchange, exchange_type="direct", delivery_mode="transient", durable=False, auto_delete=True
         )
         producer = Producer(chan, exchange=exchange)
         producer.publish(reply, routing_key=routing_key)
     finally:
         channel or chan.close()
Ejemplo n.º 13
0
    def test_publish__get(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        producer.publish({"hello": "world"})

        self.assertDictEqual(self.queue(channel).get().payload, {"hello": "world"})
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
Ejemplo n.º 14
0
    def test_purge(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        for i in range(10):
            producer.publish({"hello": "world-%s" % (i, )})

        self.assertEqual(channel._size("test_Redis"), 10)
        self.assertEqual(self.queue(channel).purge(), 10)
        channel.close()
Ejemplo n.º 15
0
    def test_purge(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        for i in range(10):
            producer.publish({"hello": "world-%s" % (i, )})

        self.assertEqual(channel._size("test_Redis"), 10)
        self.assertEqual(self.queue(channel).purge(), 10)
        channel.close()
Ejemplo n.º 16
0
    def test_publish__get(self):
        channel = self.connection.channel()
        producer = Producer(channel, self.exchange, routing_key="test_Redis")
        self.queue(channel).declare()

        producer.publish({"hello": "world"})

        self.assertDictEqual(
            self.queue(channel).get().payload, {"hello": "world"})
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
        self.assertIsNone(self.queue(channel).get())
Ejemplo n.º 17
0
 def _publish_reply(self, reply, exchange, routing_key, channel=None):
     chan = channel or self.connection.channel()
     try:
         exchange = Exchange(exchange, exchange_type="direct",
                                       delivery_mode="transient",
                                       durable=False,
                                       auto_delete=True)
         producer = Producer(chan, exchange=exchange,
                                   auto_declare=True)
         producer.publish(reply, routing_key=routing_key)
     finally:
         channel or chan.close()
Ejemplo n.º 18
0
    def producer(self):
        self.connection = BrokerConnection(hostname=AMQP_IP, port=AMQP_PORT,
                                           userid=AMQP_USER, password=AMQP_PASSWORD,
                                           virtual_host="/",
                                           transport=AMQP_TRANSPORT)
        self.channel = self.connection.channel()
        # produce

        self.media_exchange = Exchange("media", "direct", durable=True)

        producer = Producer(self.channel, exchange=self.media_exchange, serializer="json")
        producer.publish({"name": "/tmp/lolcat1.avi", "size": 1301013})

        print self.connection.get_transport_cls()
Ejemplo n.º 19
0
 def _publish(self, type, arguments, destination=None, reply_ticket=None,
         channel=None):
     message = {"method": type,
                "arguments": arguments,
                "destination": destination}
     if reply_ticket:
         message["reply_to"] = {"exchange": self.reply_exchange.name,
                                "routing_key": reply_ticket}
     chan = channel or self.connection.channel()
     producer = Producer(chan, exchange=self.exchange)
     try:
         producer.publish(message)
     finally:
         channel or chan.close()
Ejemplo n.º 20
0
    def test_produce_consume_noack(self):
        channel = self.c.channel()
        producer = Producer(channel, self.e)
        consumer = Consumer(channel, self.q, no_ack=True)

        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")

        _received = []

        def callback(message_data, message):
            _received.append(message)

        consumer.register_callback(callback)
        consumer.consume()

        while 1:
            if len(_received) == 10:
                break
            self.c.drain_events()

        self.assertEqual(len(_received), 10)
Ejemplo n.º 21
0
 def run(self):
   amqhost = self.config_parser.get(self.section,'amqhost')
   amqport = int(self.config_parser.get(self.section,'amqport'))
   amquser = self.config_parser.get(self.section,'amquser')
   amqpass = self.config_parser.get(self.section,'amqpass')
   amqvhost = self.config_parser.get(self.section,'amqvhost')
   amqexchange = self.config_parser.get(self.section,'amqexchange')
   routing_key = self.config_parser.get(self.section,'routing_key')
   connection = BrokerConnection(hostname=amqhost, port=amqport,
                           userid=amquser, password=amqpass,
                           virtual_host=amqvhost)
   channel = connection.channel()
   exchange = Exchange(amqexchange,'topic',turable=True)
   producer = Producer(channel,exchange=exchange, 
                          routing_key=routing_key)
   global entire_history
   entire_history = self.config_parser.get(self.section,'entire_history')
   for job in jobs():
      if job['type']=='start':
         job['completion_time']+=job['walltime']
      producer.revive(channel)
      producer.publish(job)
      logging.debug(`job`)
Ejemplo n.º 22
0
    def test_publish__consume(self):
        connection = Connection(transport=Transport)
        channel = connection.channel()
        producer = Producer(channel, self.exchange, routing_key='test_Redis')
        consumer = Consumer(channel, self.queue)

        producer.publish({'hello2': 'world2'})
        _received = []

        def callback(message_data, message):
            _received.append(message_data)
            message.ack()

        consumer.register_callback(callback)
        consumer.consume()

        self.assertIn(channel, channel.connection.cycle._channels)
        try:
            connection.drain_events(timeout=1)
            self.assertTrue(_received)
            with self.assertRaises(socket.timeout):
                connection.drain_events(timeout=0.01)
        finally:
            channel.close()
Ejemplo n.º 23
0
    def handle(self, *args, **options):
        routing_key = options["routing_key"]
        connection_url = options["connection_url"]
        userid = urlparse(connection_url).username
        payload_file = options["payload_file"]

        exchange_name = "exchange/{}/jobs".format(userid)

        connection = Connection(connection_url)
        exchange = Exchange(exchange_name, type="topic")
        producer = Producer(connection,
                            exchange,
                            routing_key=routing_key,
                            auto_declare=True)

        self.stdout.write("Published to exchange: {}".format(exchange_name))

        with open(payload_file) as f:
            body = f.read()

            try:
                producer.publish(body)
            finally:
                connection.release()
Ejemplo n.º 24
0
    def handle(self, *args, **options):
        routing_key = options["routing_key"]
        connection_url = options["connection_url"]
        userid = urlparse(connection_url).username
        payload_file = options["payload_file"]

        exchange_name = "exchange/{}/jobs".format(userid)

        connection = Connection(connection_url)
        exchange = Exchange(exchange_name, type="topic")
        producer = Producer(connection,
                            exchange,
                            routing_key=routing_key,
                            auto_declare=True)

        self.stdout.write("Published to exchange: {}".format(exchange_name))

        with open(payload_file) as f:
            body = f.read()

            try:
                producer.publish(body)
            finally:
                connection.release()
Ejemplo n.º 25
0
    def test_publish(self):
        channel = self.connection.channel()
        p = Producer(channel, self.exchange, serializer="json")
        message = {u"the quick brown fox": u"jumps over the lazy dog"}
        ret = p.publish(message, routing_key="process")
        self.assertIn("prepare_message", channel)
        self.assertIn("basic_publish", channel)

        m, exc, rkey = ret
        self.assertDictEqual(message, anyjson.loads(m["body"]))
        self.assertDictContainsSubset({"content_type": "application/json",
                                       "content_encoding": "utf-8",
                                       "priority": 0}, m)
        self.assertDictContainsSubset({"delivery_mode": 2}, m["properties"])
        self.assertEqual(exc, p.exchange.name)
        self.assertEqual(rkey, "process")
Ejemplo n.º 26
0
    def test_publish(self):
        channel = self.connection.channel()
        p = Producer(channel, self.exchange, serializer='json')
        message = {u'the quick brown fox': u'jumps over the lazy dog'}
        ret = p.publish(message, routing_key='process')
        self.assertIn('prepare_message', channel)
        self.assertIn('basic_publish', channel)

        m, exc, rkey = ret
        self.assertDictEqual(message, anyjson.loads(m['body']))
        self.assertDictContainsSubset({'content_type': 'application/json',
                                       'content_encoding': 'utf-8',
                                       'priority': 0}, m)
        self.assertDictContainsSubset({'delivery_mode': 2}, m['properties'])
        self.assertEqual(exc, p.exchange.name)
        self.assertEqual(rkey, 'process')
Ejemplo n.º 27
0
    def test_publish(self):
        channel = self.connection.channel()
        p = Producer(channel, self.exchange, serializer="json")
        message = {u"the quick brown fox": u"jumps over the lazy dog"}
        ret = p.publish(message, routing_key="process")
        self.assertIn("prepare_message", channel)
        self.assertIn("basic_publish", channel)

        m, exc, rkey = ret
        self.assertDictEqual(message, anyjson.loads(m["body"]))
        self.assertDictContainsSubset(
            {
                "content_type": "application/json",
                "content_encoding": "utf-8",
                "priority": 0
            }, m)
        self.assertDictContainsSubset({"delivery_mode": 2}, m["properties"])
        self.assertEqual(exc, p.exchange.name)
        self.assertEqual(rkey, "process")
Ejemplo n.º 28
0
    def run(self):
        
        print 'pypo Pusher'
        if self.action == 'update_schedule':
            print 'update_schedule!!'
            credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)
            connection = pika.BlockingConnection(pika.ConnectionParameters(MQ_HOST,
                                       5672,
                                       '/airtime',
                                       credentials))
            channel = connection.channel()
            channel.queue_declare(queue='pypo-fetch', durable=True)
            message = {
                       'schedule': {
                                    'media': {}
                                    },
                       'event_type': 'update_schedule'
                       }
            
            import json
            message = json.dumps(message)
            
            message = 'hallo'
            
            channel.basic_publish(exchange='airtime-pypo',
                      routing_key='pypo-fetch',
                      body=message)
            
            channel.close()
            connection.close()


            
            
        if self.action == 'update_schedule_kombu':
            print 'update_schedule!!'
            
            exchange = Exchange("airtime-pypo", "direct", durable=True, auto_delete=True)
            queue = Queue("pypo-fetch", exchange=exchange, key="foo", durable=True)
            
            connection = BrokerConnection(MQ_HOST, MQ_USER, MQ_PASS, MQ_VHOST)
            channel = connection.channel()
            
            simple_queue = SimpleQueue(channel, queue)
            
            
            
            
            
            message = {
                       'schedule': {
                                    'media': {}
                                    },
                       'event_type': 'update_schedule'
                       }
            
            
            print simple_queue.qsize()
            
            print 'do:'
            
            
            producer = Producer(channel, exchange=exchange, routing_key=None, serializer="json")
            
            
            
            producer.publish(message, routing_key='pypo-fetch')
            
            
            print simple_queue.qsize()
            channel.close()
Ejemplo n.º 29
0
class UdpService():
    def __init__(self):
        config = ConfigParser.ConfigParser()
        config.read('udpagent.conf')
        self.udp_address      =  config.get('udpservice','udp_address')
        self.udp_port         =  int(config.get('udpservice','udp_port')) 
        self.rabbit_user      =  config.get('udpservice','rabbit_userid')
        self.rabbit_password  =  config.get('udpservice','rabbit_password') 
        self.rabbit_host      =  config.get('udpservice','rabbit_hosts')
        self.acord_control_exchange = config.get('udpservice','acord_control_exchange') 
        logging.config.fileConfig('udpagent.conf', disable_existing_loggers=False)
    def printconfig(self):
        logging.debug("udp_address:%s",self.udp_address) 
        logging.debug("udp_port:%s",self.udp_port)  
        logging.debug("rabbit_user:%s",self.rabbit_user)  
        logging.debug("rabbit_password:%s",self.rabbit_password)  
        logging.debug("rabbit_hosts:%s",self.rabbit_host)  
        logging.debug("cord_control_exchange:%s",self.acord_control_exchange)
   
    def convert_sample_to_event_data(self,msg):
        event_data = {'event_type': 'infra','message_id':six.text_type(uuid.uuid4()),'publisher_id': 'cpe_publisher_id','timestamp':datetime.datetime.now().isoformat(),'priority':'INFO','payload':msg}
        return event_data
   
    def setup_rabbit_mq_channel(self):
        service_exchange = Exchange(self.acord_control_exchange, "topic", durable=False)
        # connections/channels
        connection = BrokerConnection(self.rabbit_host, self.rabbit_user, self.rabbit_password)
        logging.info("Connection to RabbitMQ server successful")
        channel = connection.channel()
        # produce
        self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')

    def start_udp(self):
        address_family = socket.AF_INET
        if netutils.is_valid_ipv6(self.udp_address):
            address_family = socket.AF_INET6
        udp = socket.socket(address_family, socket.SOCK_DGRAM)
        udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        udp.bind((self.udp_address,
                  self.udp_port))

        self.setup_rabbit_mq_channel()
        self.udp_run = True
        while self.udp_run:
            # NOTE(jd) Arbitrary limit of 64K because that ought to be
            # enough for anybody.
            data, source = udp.recvfrom(64 * units.Ki)
            try:
                sample = msgpack.loads(data, encoding='utf-8')
            except Exception:
                logging.warning("UDP: Cannot decode data sent by %s", source)
            else:
                try:
                    if sample.has_key("event_type"):
                         logging.debug("recevied event  :%s",sample)
                         self.producer.publish(sample)
                    else:
                         logging.debug("recevied Sample  :%s",sample)
                         msg = self.convert_sample_to_event_data(sample)
                         self.producer.publish(msg)
                except Exception:
                    logging.exception("UDP: Unable to publish msg")
Ejemplo n.º 30
0
#coding:utf-8
import time
import random
import json

from kombu import Connection
from kombu.messaging import Producer
from kombu import Exchange


connection = Connection(hostname="192.168.10.201",virtual_host='/websocketserver')
channel = connection.channel()
publish_exchange = Producer(channel,routing_key='sys.sendweixin')


publish_exchange.publish(json.dumps({
    'weixin_users':['o8Td4joS0bB4fMekczcIoH1id7Qc'],
    'body':{
        "msgtype":"text",
        "text":
        {
             "content":"完整测试"
        }
    }
}))

Ejemplo n.º 31
0
 def producer(self):
     producer = Producer(self.channel, exchange=self.media_exchange, serializer="json")
     #producer.exchange.delete()
     producer.publish({"name": "/tmp/lolcat1.avi", "size": 1301013})
     pass
Ejemplo n.º 32
0
__author__ = 'amen'
import time
import random

from kombu import Connection
from kombu.messaging import Producer
from kombu import Exchange

connection = Connection(hostname="192.173.1.213",
                        virtual_host='/websocketserver')
channel = connection.channel()
exchange = Exchange("sys.apn",
                    type='topic',
                    channel=channel,
                    durable=True,
                    delivery_mode=2)
publish_exchange = Producer(channel, exchange, routing_key='msg.debug')

while True:
    publish_exchange.publish(
        "body",
        headers={
            "message":
            "you id code:%d" % random.randint(100, 100000),
            "uhid":
            "d8009e6c8e074d1bbcb592f321367feaef5674a82fc4cf3b78b066b7c8ad59bd"
        })
    time.sleep(5)
Ejemplo n.º 33
0
'''
Created on May 19, 2014

@author: xiaoxubeii
'''
from kombu import Connection
from kombu.messaging import Producer
from entity1 import exchange
#from kombu.transport.base import Message

connection = Connection("amqp://*****:*****@xiaoxubeii")
channel = connection.channel()
#message = Message(channel, body='hi, i am a test')
producer = Producer(channel, exchange=exchange)
producer.publish('hi, i am a test', route_key='test')
Ejemplo n.º 34
0
'''
Created on May 13, 2014

@author: xiaoxubeii
'''
from kombu import Connection  
from kombu.messaging import Producer  
from entity import task_exchange  
from kombu.transport.base import Message  
  
connection = Connection('amqp://*****:*****@localhost:5672//')  
channel = connection.channel()  
  
message = Message(channel, body='Hello Kombu')  
  
# produce  
producer = Producer(channel, exchange=task_exchange)  
producer.publish(message.body, routing_key='suo_piao')
Ejemplo n.º 35
0
from kombu.entity import Exchange
from kombu.messaging import Producer
from kombu.connection import Connection
from time import sleep

connection = Connection('amqp://*****:*****@192.168.8.108:5672//')
channel = connection.channel()

_exchange = Exchange('media', 'direct', channel)
producer = Producer(channel, exchange=_exchange, routing_key='video')
for i in range(10):
    producer.publish({'name': 'fuckyou.avi', 'size': 13131})
    print 'published!'
    sleep(1)
Ejemplo n.º 36
0
Archivo: 1.py Proyecto: sjl421/codes
 def produce_msg():
     producer = Producer(channel1, exchange=_exchange, routing_key='c9')
     for _ in range(5):
         producer.publish({'name': 'xjtu', 'status': 'c9'})
         sleep(1)
Ejemplo n.º 37
0
# send.py

from kombu import Connection
from kombu.messaging import Producer
from entity import task_exchange
from kombu.transport.base import Message

connection = Connection('amqp://*****:*****@10.120.120.11:5672//')
channel = connection.channel()

message=Message(channel,body='Hello Kombu')

# produce
producer = Producer(channel,exchange=task_exchange)
producer.publish(message.body,routing_key='suo_piao')
import ipdb; ipdb.set_trace() ### XXX BREAKPOINT

Ejemplo n.º 38
0
def send_message(msg):

    exchange = Exchange(name=exchange_name, type='topic',
                        exclusive=False, durable=False, auto_delete=False)
    p = Producer(conn.channel(), exchange, routing_key=routing_key)
    p.publish(msg)
Ejemplo n.º 39
0
import json

__author__ = 'amen'
import time
import random

from kombu import Connection
from kombu.messaging import Producer
from kombu import Exchange


connection = Connection(hostname="192.173.1.213",virtual_host='/websocketserver')
channel = connection.channel()
exchange=Exchange("sys.sms",type='topic',channel=channel,durable=True,delivery_mode=2)
publish_exchange = Producer(channel,exchange,routing_key='sms.code')

while True:
    publish_exchange.publish(json.dumps({'content':"code %d"%random.randint(100,500),'phone':"15034445667"}))
    time.sleep(5)
Ejemplo n.º 40
0
def produce_msg_image(channel, image_name, image, queue_name):
    private_exchange = Exchange(queue_name, "direct", durable=True)
    producer = Producer(channel, exchange=private_exchange, serializer="pickle")
    producer.publish(document, routing_key=queue_name)
    logger.debug("message produced")
Ejemplo n.º 41
0
class EventDispatcher(object):
    """Send events as messages.

    :param connection: Connection to the broker.

    :keyword hostname: Hostname to identify ourselves as,
        by default uses the hostname returned by :func:`socket.gethostname`.

    :keyword enabled: Set to :const:`False` to not actually publish any events,
        making :meth:`send` a noop operation.

    :keyword channel: Can be used instead of `connection` to specify
        an exact channel to use when sending events.

    :keyword buffer_while_offline: If enabled events will be buffered
       while the connection is down. :meth:`flush` must be called
       as soon as the connection is re-established.

    You need to :meth:`close` this after use.

    """

    def __init__(self, connection=None, hostname=None, enabled=True,
            channel=None, buffer_while_offline=True, app=None):
        self.app = app_or_default(app)
        self.connection = connection
        self.channel = channel
        self.hostname = hostname or socket.gethostname()
        self.enabled = enabled
        self.buffer_while_offline = buffer_while_offline
        self._lock = threading.Lock()
        self.publisher = None
        self._outbound_buffer = deque()

        if self.enabled:
            self.enable()

    def enable(self):
        conf = self.app.conf
        self.enabled = True
        channel = self.channel or self.connection.channel()
        self.publisher = Producer(channel,
                                  exchange=event_exchange,
                                  serializer=conf.CELERY_EVENT_SERIALIZER)

    def disable(self):
        self.enabled = False
        if self.publisher is not None:
            if not self.channel:  # close auto channel.
                self.publisher.channel.close()
            self.publisher = None

    def send(self, type, **fields):
        """Send event.

        :param type: Kind of event.
        :keyword \*\*fields: Event arguments.

        """
        if not self.enabled:
            return

        self._lock.acquire()
        event = Event(type, hostname=self.hostname, **fields)
        try:
            try:
                self.publisher.publish(event,
                                       routing_key=type.replace("-", "."))
            except Exception, exc:
                if not self.buffer_while_offline:
                    raise
                self._outbound_buffer.append((type, fields, exc))
        finally:
            self._lock.release()

    def flush(self):
        while self._outbound_buffer:
            type, fields, _ = self._outbound_buffer.popleft()
            self.send(type, **fields)

    def close(self):
        """Close the event dispatcher."""
        self._lock.locked() and self._lock.release()
        self.publisher and self.publisher.channel.close()
Ejemplo n.º 42
0
with Connection(
        'amqp://*****:*****@10.46.0.39:5672/rapid_alpha_dev') as connection:
    with connection.channel() as channel:
        for i in range(1, 10):
            science_news = Queue(
                name='kombu_queue',
                exchange=Exchange('kombu_queue', type='direct'),
                routing_key='kombu_queue',
                channel=channel,
                durable=False,
            )
            science_news.declare()
            producer = Producer(channel,
                                serializer='json',
                                routing_key='kombu_queue')
            producer.publish({'name': 'kombu_queue', 'size': i})

            science_news = Queue(
                name='kombu_queue_1',
                exchange=Exchange('kombu_queue_1', type='direct'),
                routing_key='kombu_queue_1',
                channel=channel,
                max_priority=10,  # 优先级
                durable=False,
            )
            science_news.declare()
            producer = Producer(channel,
                                serializer='json',
                                routing_key='kombu_queue_1')
            producer.publish({'name': 'kombu_queue_1', 'size': i}, priority=i)
Ejemplo n.º 43
0
class EventDispatcher(object):
    """Send events as messages.

    :param connection: Connection to the broker.

    :keyword hostname: Hostname to identify ourselves as,
        by default uses the hostname returned by :func:`socket.gethostname`.

    :keyword enabled: Set to :const:`False` to not actually publish any events,
        making :meth:`send` a noop operation.

    :keyword channel: Can be used instead of `connection` to specify
        an exact channel to use when sending events.

    :keyword buffer_while_offline: If enabled events will be buffered
       while the connection is down. :meth:`flush` must be called
       as soon as the connection is re-established.

    You need to :meth:`close` this after use.

    """
    def __init__(self,
                 connection=None,
                 hostname=None,
                 enabled=True,
                 channel=None,
                 buffer_while_offline=True,
                 app=None):
        self.app = app_or_default(app)
        self.connection = connection
        self.channel = channel
        self.hostname = hostname or socket.gethostname()
        self.enabled = enabled
        self.buffer_while_offline = buffer_while_offline
        self._lock = threading.Lock()
        self.publisher = None
        self._outbound_buffer = deque()

        if self.enabled:
            self.enable()

    def enable(self):
        conf = self.app.conf
        self.enabled = True
        channel = self.channel or self.connection.channel()
        self.publisher = Producer(channel,
                                  exchange=event_exchange,
                                  serializer=conf.CELERY_EVENT_SERIALIZER)

    def disable(self):
        self.enabled = False
        if self.publisher is not None:
            if not self.channel:  # close auto channel.
                self.publisher.channel.close()
            self.publisher = None

    def send(self, type, **fields):
        """Send event.

        :param type: Kind of event.
        :keyword \*\*fields: Event arguments.

        """
        if not self.enabled:
            return

        self._lock.acquire()
        event = Event(type, hostname=self.hostname, **fields)
        try:
            try:
                self.publisher.publish(event,
                                       routing_key=type.replace("-", "."))
            except Exception, exc:
                if not self.buffer_while_offline:
                    raise
                self._outbound_buffer.append((type, fields, exc))
        finally:
            self._lock.release()

    def flush(self):
        while self._outbound_buffer:
            type, fields, _ = self._outbound_buffer.popleft()
            self.send(type, **fields)

    def copy_buffer(self, other):
        self._outbound_buffer = other._outbound_buffer

    def close(self):
        """Close the event dispatcher."""
        self._lock.locked() and self._lock.release()
        self.publisher and self.publisher.channel.close()
Ejemplo n.º 44
0
from kombu import Connection
from kombu.messaging import Producer
from kombu.transport.base import Message
from kombu_queues import task_exchange
from kombu_tasks import echo_task

rabbitmq_url = 'amqp://*****:*****@localhost:5672//'
message_num = 400

if __name__ == '__main__':
    connection = Connection(rabbitmq_url)
    channel = connection.channel()

    body_json = {'url': 'http://127.0.0.1',
                 'delay': 5}
    message_json = Message(channel, body=body_json)

    body_pickle = {'func': echo_task,
                   'args': ('Hello Rabbit', 5),
                   'kwargs': {}}
    message_pickle = Message(channel, body=body_pickle)

    producer_json = Producer(channel, exchange=task_exchange)
    producer_pickle = Producer(channel, exchange=task_exchange, serializer='pickle')
    for i in xrange(message_num):
        producer_json.publish(message_json.body, routing_key='json_queue')
        producer_pickle.publish(message_pickle.body, routing_key='pickle_queue')
Ejemplo n.º 45
0
class EventDispatcher(object):
    """Send events as messages.

    :param connection: Connection to the broker.

    :keyword hostname: Hostname to identify ourselves as,
        by default uses the hostname returned by :func:`socket.gethostname`.

    :keyword enabled: Set to :const:`False` to not actually publish any events,
        making :meth:`send` a noop operation.

    :keyword channel: Can be used instead of `connection` to specify
        an exact channel to use when sending events.

    :keyword buffer_while_offline: If enabled events will be buffered
       while the connection is down. :meth:`flush` must be called
       as soon as the connection is re-established.

    You need to :meth:`close` this after use.

    """
    def __init__(self,
                 connection=None,
                 hostname=None,
                 enabled=True,
                 channel=None,
                 buffer_while_offline=True,
                 app=None,
                 serializer=None):
        self.app = app_or_default(app)
        self.connection = connection
        self.channel = channel
        self.hostname = hostname or socket.gethostname()
        self.buffer_while_offline = buffer_while_offline
        self.mutex = threading.Lock()
        self.publisher = None
        self._outbound_buffer = deque()
        self.serializer = serializer or self.app.conf.CELERY_EVENT_SERIALIZER
        self.on_enabled = set()
        self.on_disabled = set()

        self.enabled = enabled
        if self.enabled:
            self.enable()

    def __enter__(self):
        return self

    def __exit__(self, *exc_info):
        self.close()

    def enable(self):
        self.publisher = Producer(self.channel or self.connection.channel(),
                                  exchange=event_exchange,
                                  serializer=self.serializer)
        self.enabled = True
        for callback in self.on_enabled:
            callback()

    def disable(self):
        if self.enabled:
            self.enabled = False
            self.close()
            for callback in self.on_disabled:
                callback()

    def send(self, type, **fields):
        """Send event.

        :param type: Kind of event.
        :keyword \*\*fields: Event arguments.

        """
        if self.enabled:
            with self.mutex:
                event = Event(type,
                              hostname=self.hostname,
                              clock=self.app.clock.forward(),
                              **fields)
                try:
                    self.publisher.publish(event,
                                           routing_key=type.replace("-", "."))
                except Exception, exc:
                    if not self.buffer_while_offline:
                        raise
                    self._outbound_buffer.append((type, fields, exc))
Ejemplo n.º 46
0
 def queue_publish(self, routing_key,
                   exchange_name, exchange_type='topic', body=None):
     exchange = Exchange(name=exchange_name, type=exchange_type,
                         exclusive=False, durable=False, auto_delete=False)
     p = Producer(self._channel(), exchange, routing_key=routing_key)
     return p.publish(body)
Ejemplo n.º 47
0
        self.consumer.consume()

        def mq_callback(message_data, message):
            print("%s: %r: %r" % (key, message.delivery_info, message_data,))
            #message.ack()
        self.consumer.register_callback(mq_callback)

c1 = AConsumer("test_1","test.1")
c2 = AConsumer("testing","test.ing")
# consumers can use simple pattern matching when defining a queue
c3 = AConsumer("test_all","test.*")

# 3. publish something to consume
# publishers always send to a specific route, the mq will route to the queues
producer = Producer(pub_chan, exchange=pub_exch, serializer=message_serializer)
producer.publish({"name": "Shane Caraveo", "username": "******"}, routing_key="test.1")
producer.publish({"name": "Micky Mouse", "username": "******"}, routing_key="test.ing")
producer.publish({"name": "Anonymous", "username": "******"}, routing_key="test.foobar")

def have_messages():
    return sum([q.qsize() for q in cons_chan.queues.values()])

# 5. run the event loop
while have_messages():
    try:
        cons_conn.drain_events()
    except KeyboardInterrupt:
        print
        print "quitting"
        break
    except Exception, e:
Ejemplo n.º 48
0
__author__ = 'amen'
import time
import random

from kombu import Connection
from kombu.messaging import Producer
from kombu import Exchange


connection = Connection(hostname="192.173.1.213",virtual_host='/testDemo')
channel = connection.channel()
exchange=Exchange("exchange1",type='topic',channel=channel,durable=True,delivery_mode=2)
publish_exchange = Producer(channel,exchange,routing_key='all')

while True:
    publish_exchange.publish("body",headers={"message":"you id code:%d"%random.randint(100,100000),
			      "uhid":"d8009e6c8e074d1bbcb592f321367feaef5674a82fc4cf3b78b066b7c8ad59bd","badge":5})
    time.sleep(5)
Ejemplo n.º 49
0
class EventDispatcher(object):
    """Send events as messages.

    :param connection: Connection to the broker.

    :keyword hostname: Hostname to identify ourselves as,
        by default uses the hostname returned by :func:`socket.gethostname`.

    :keyword enabled: Set to :const:`False` to not actually publish any events,
        making :meth:`send` a noop operation.

    :keyword channel: Can be used instead of `connection` to specify
        an exact channel to use when sending events.

    :keyword buffer_while_offline: If enabled events will be buffered
       while the connection is down. :meth:`flush` must be called
       as soon as the connection is re-established.

    You need to :meth:`close` this after use.

    """

    def __init__(self, connection=None, hostname=None, enabled=True,
            channel=None, buffer_while_offline=True, app=None,
            serializer=None):
        self.app = app_or_default(app)
        self.connection = connection
        self.channel = channel
        self.hostname = hostname or socket.gethostname()
        self.buffer_while_offline = buffer_while_offline
        self.mutex = threading.Lock()
        self.publisher = None
        self._outbound_buffer = deque()
        self.serializer = serializer or self.app.conf.CELERY_EVENT_SERIALIZER
        self.on_enabled = set()
        self.on_disabled = set()

        self.enabled = enabled
        if not connection and channel:
            self.connection = channel.connection.client
        if self.enabled:
            self.enable()

    def __enter__(self):
        return self

    def __exit__(self, *exc_info):
        self.close()

    def get_exchange(self):
        if self.connection:
            return get_exchange(self.connection)
        else:
            return get_exchange(self.channel.connection.client)

    def enable(self):
        self.publisher = Producer(self.channel or self.connection,
                                  exchange=self.get_exchange(),
                                  serializer=self.serializer)
        self.enabled = True
        for callback in self.on_enabled:
            callback()

    def disable(self):
        if self.enabled:
            self.enabled = False
            self.close()
            for callback in self.on_disabled:
                callback()

    def send(self, type, **fields):
        """Send event.

        :param type: Kind of event.
        :keyword \*\*fields: Event arguments.

        """
        if self.enabled:
            with self.mutex:
                event = Event(type, hostname=self.hostname,
                                    clock=self.app.clock.forward(), **fields)
                try:
                    self.publisher.publish(event,
                                           routing_key=type.replace("-", "."))
                except Exception, exc:
                    if not self.buffer_while_offline:
                        raise
                    self._outbound_buffer.append((type, fields, exc))
Ejemplo n.º 50
0
from kombu.messaging import Producer
from kombu.transport.base import Message
from kombu_queues import task_exchange
from kombu_tasks import echo_task

rabbitmq_url = 'amqp://*****:*****@localhost:5672//'
message_num = 400

if __name__ == '__main__':
    connection = Connection(rabbitmq_url)
    channel = connection.channel()

    body_json = {'url': 'http://127.0.0.1', 'delay': 5}
    message_json = Message(channel, body=body_json)

    body_pickle = {
        'func': echo_task,
        'args': ('Hello Rabbit', 5),
        'kwargs': {}
    }
    message_pickle = Message(channel, body=body_pickle)

    producer_json = Producer(channel, exchange=task_exchange)
    producer_pickle = Producer(channel,
                               exchange=task_exchange,
                               serializer='pickle')
    for i in xrange(message_num):
        producer_json.publish(message_json.body, routing_key='json_queue')
        producer_pickle.publish(message_pickle.body,
                                routing_key='pickle_queue')