-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
80 lines (49 loc) · 1.85 KB
/
client.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
# -*- coding: UTF-8 -*-
import rpyc
import CommandLineParser
import ConfigFileParser
class RPCClient:
def __init__(self, host, port, login, password):
channel = rpyc.Channel(rpyc.SocketStream.connect(host, port))
secure = rpyc.core.brine.dump( (login,password) )
channel.send( secure )
response = channel.recv()
if response == 'AUTH_ERROR':
raise ValueError('Invalid login or password for daemon')
self.conn = rpyc.utils.factory.connect_channel(channel)
def foo(self):
print self.conn.root.get_answer()
def get(self, key):
return self.conn.root.get(key)
def put(self, key, val):
return self.conn.root.put(key, val)
def update(self, key, val):
return self.conn.root.update(key, val)
if __name__ == '__main__':
try:
config_parser = ConfigFileParser.ConfigFileParser()
host = config_parser.host()
port = int( config_parser.port() )
login = config_parser.login()
password = config_parser.password()
client = RPCClient( host, port, login, password )
clp = CommandLineParser.CommandLineParser()
if clp.get_method() == 'get':
key = clp.get_args()
res = client.get(key)[0]
if res:
print 'ID: %s, KEY: %s, DATA: %s' % (res[0],res[1],res[2])
else:
print 'Nothing select'
elif clp.get_method() == 'put':
key, val = clp.put_args()
client.put(key, val)
print "Success insert"
elif clp.get_method() == 'upd':
key, val = clp.upd_args()
rowcount = client.update(key, val)
print 'Updated rows: %d' % rowcount
else:
print 'Error method!: ', clp.get_method()
except Exception as e:
print e