This repository was archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdcli
More file actions
executable file
·93 lines (68 loc) · 2.85 KB
/
Copy pathdcli
File metadata and controls
executable file
·93 lines (68 loc) · 2.85 KB
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
#!/usr/bin/env python3
"""
DCD Client Interface (dcli)
"""
import zmq
import logging
import optparse
import os
parser = optparse.OptionParser()
parser.add_option("-s", "--server", dest="server",
help="URI of alternate server to communicate with", default="tcp://127.0.0.1:5561")
parser.add_option("-g", "--get", dest="get", action="store_true", help="Send a data query the server")
parser.add_option("-p", "--put", dest="put", action="store_true", help="Set the data value on the server")
parser.add_option("-k", "--key", dest="key", help="key to process")
parser.add_option("-r", "--raw", dest="raw", help="debug only - send raw string to server")
parser.add_option("-v", "--value", dest="value", help="key to process")
parser.add_option("-e", "--echo_test", dest="test", help="Send an echo test to the server")
parser.add_option("-l", "--link", dest="link", help="Link this server to another")
parser.add_option("-d", "--debug", dest="debug", action="store_true", help="Print debug information to the screen")
parser.add_option("--dump", dest="dump", action="store_true", help="Show all values in the database")
(options, args) = parser.parse_args()
if options.debug is None:
options.stream_log_level = logging.INFO
else:
options.stream_log_level = logging.DEBUG
# look up process name, pid
scriptname = os.path.basename(__file__).split(".py")[0]
pid = os.getpid()
# define where to write the logs
log_path = 'log/{}.log'.format(scriptname)
# define a stream and file handler
log = logging.getLogger()
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(module)s %(message)s')
stream_handler = logging.StreamHandler()
stream_handler.setLevel(options.stream_log_level)
stream_handler.setFormatter(formatter)
file_handler = logging.FileHandler(filename = log_path)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
log.addHandler(file_handler)
log.addHandler(stream_handler)
# Prepare our context and sockets
context = zmq.Context()
#log.debug("connecting to {}".format(options.server))
me = context.socket(zmq.REQ)
me.connect(options.server)
def build_message(command, key, value):
return repr((command, key, value))
def send_message(message):
me.send_string(message)
log.info("{} sent {} to {}".format(pid, message, options.server))
in_message = me.recv_string()
log.info("{} received: ".format(pid) + str(in_message))
if not args:
print("please enter a command")
if options.get:
send_message(build_message("get", options.key, ""))
if options.put:
send_message(build_message("put", options.key, options.value))
if options.test:
send_message(build_message("echo_test", "", options.value))
if options.link:
send_message(build_message("link", "", options.link))
if options.dump:
send_message(build_message("dump", "", ""))
if options.raw:
send_message(options.raw)