-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcct.py
146 lines (113 loc) · 3.85 KB
/
cct.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!python
# readme
# written by Jidor Tang <tlqtangok@126.com> , 2018-12-15
# a tiny command line chat tools based redis, python3
# dependency: redis ,
# . run "pip3 install redis" to install
# steps
# . login use an id
# . CTRL+C to send msg
import redis
import time
import os
from multiprocessing import Process
import signal
import datetime
import pickle
import sys
### def list ###
global flag_check_msg
flag_check_msg = [1] # default check msg
# utils_ begin
def get_timestamp():
dt = datetime.datetime.now()
return dt
def chomp(id_str):
return id_str.strip()
# utils_ end
def connect_r(host, port=6379):
return redis.Redis(host=host, port=port, db=0)
def signal_handler_sys_exit(sig, frame):
sys.exit(1)
def signal_handler_send_msg(sig, frame):
flag_check_msg[0] = 0
def login_as():
id_ = input("- login as: ")
id_ = chomp(id_)
assert( id_ != "")
return id_
def create_msg_from_whom(id_):
timestamp = get_timestamp()
from_whom = id_
to_whom = "NULL"
msg_body = ""
to_whom = input("- send msg to whom? ")
to_whom = chomp(to_whom)
print("- input your msg content, use END to end input \nBEGIN\n")
e_l = ""
while e_l != "END":
msg_body += e_l + "\n"
e_l = input("> ")
msg_pkg = {
"timestamp": timestamp,
"from":from_whom,
"to":to_whom,
"msg_body":msg_body
}
msg_pickle_string = pickle.dumps(msg_pkg)
return [msg_pkg, msg_pickle_string]
def pretty_msg_pkg(msg_pkg):
ret_str = ""
my_id_ = msg_pkg["to"]
from_ = msg_pkg["from"]
msg_body = msg_pkg["msg_body"]
timestamp = msg_pkg["timestamp"]
timestring = timestamp.strftime("%Y-%m-%d %H:%M")
ret_str = "\n------ " + timestring + " msg from " + from_ + " ------" + msg_body + "\n"
return ret_str
def check_msg_to_me(con, my_id_):
MSG_PREFIX = "MSG_TO_"
msg_pickle_string = con.lpop(MSG_PREFIX + my_id_)
if msg_pickle_string != None:
msg_pkg = pickle.loads(msg_pickle_string)
print(pretty_msg_pkg(msg_pkg))
return msg_pickle_string
def send_msg_to_whom(con, msg_pickle_string, msg_pkg):
MSG_PREFIX = "MSG_TO_"
con.rpush(MSG_PREFIX + msg_pkg["to"] , msg_pickle_string)
return "- send msg from " + msg_pkg["from"] + " to " + msg_pkg["to"] + ", done!"
### end def ###
if __name__ == "__main__":
if len(sys.argv) == 2 and (sys.argv[1] == "-V" or sys.argv[1] == "-v"):
#print (sys.argv[1])
print ("cct\tCommand-line Chat Tools\nversion 1.0\n\nwritten by Jidor Tang<tlqtangok@126.com> at 2018-12-15")
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler_send_msg)
#signal.signal(signal.SIGQUIT, signal_handler_sys_exit)
host = "localhost"
port = 6379
con = connect_r(host)
id_ = login_as()
print ("login as " + id_)
cnt_check = 0
print("- press [CTRL + C] to send msg !")
while True:
ck_status = None
if flag_check_msg[0]:
ck_status = check_msg_to_me(con, id_)
else:
flag_check_msg[0] = 1
cnt_check = 0
s_or_r = input("\n- do you want to send msg Y(YES) | N(NO)?: ")
s_or_r = chomp(s_or_r)
if s_or_r == "YES" or s_or_r == "Y" or s_or_r == "yes" or s_or_r == "y":
[msg_pkg, msg_pickle_string] = create_msg_from_whom(id_)
send_res = send_msg_to_whom(con, msg_pickle_string, msg_pkg)
print (send_res)
if ck_status == None and cnt_check % 25 == 0:
print (".", flush=True,end="")
cnt_check += 1
if cnt_check == 0.5 * 60 * 60:
sys.exit(1)
time.sleep(1)
con.connection_pool.disconnect()