forked from hackyourlife/orakel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalk.py
executable file
·40 lines (33 loc) · 939 Bytes
/
talk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/python
# -*- coding: utf-8 -*-
# vim:set ts=8 sts=8 sw=8 tw=80 noet cc=80:
import sys
import json
from messaging import open_connection, ROUTING_KEY_SENDMUC
if __name__ == "__main__":
connection, exchange = open_connection()
channel = connection.channel()
channel.exchange_declare(exchange=exchange, type="direct")
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange=exchange, queue=queue_name,
routing_key=ROUTING_KEY_SENDMUC)
def send(key, data):
channel.basic_publish(exchange=exchange,
routing_key=key,
body=json.dumps(data).encode('utf-8'))
def send_muc(msg):
send(ROUTING_KEY_SENDMUC, msg)
try:
while True:
sys.stdout.write('> ')
sys.stdout.flush()
line = sys.stdin.readline()
if not line:
break
msg = line.strip()
if len(msg) == 0:
continue
send_muc(msg)
except KeyboardInterrupt: pass
connection.close()