-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverManager.py
More file actions
120 lines (99 loc) · 4.11 KB
/
Copy pathserverManager.py
File metadata and controls
120 lines (99 loc) · 4.11 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
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
import os
import sys
import cPickle as Pickle
from twisted.internet import reactor
from twisted.spread import pb
from twisted.cred import credentials
from appmanager import log
from twisted.internet.error import DNSLookupError, ReactorNotRestartable, ReactorAlreadyRunning
clients = []
def runKlass(details):
username, password = details
creds = credentials.UsernamePassword(username, password)
tester = Messanger(creds)
tester.runEngine()
try:
reactor.run()
except ReactorNotRestartable, e:
print e
factory = pb.PBClientFactory()
reactor.connectTCP("server", 13333, factory)
except ReactorAlreadyRunning, e:
pass
class Messanger(object):
def __init__(self, credentials):
self.credentials = credentials
self.server = None
self.clients = []
self.host = self.hostGetter()
def hostGetter(self):
if os.path.exists("bin/read"):
with open("bin/read", "r") as f:
try:
return f.readlines()[2].replace("\n", '')
except IndexError:
return "server"
else:
return "server"
def runEngine(self):
self.connect().addCallback(
lambda _: self.get_company_details()).addCallback(
lambda _: self.get_clients_with_balance()).addCallback(
lambda _: self.get_clients_with_renewal()).addCallback(
lambda _: self.get_clients_with_expiry()).addCallback(
lambda _: self.get_all_clients()).addCallback(
self._catchFailure).addCallback(
lambda _: reactor.stop())
def connect(self):
factory = pb.PBClientFactory()
reactor.connectTCP(self.host, 13333, factory)
return factory.login(self.credentials).addCallback(self._connect)
def _connect(self, rootObj):
self.server = rootObj
def _catchFailure(self, failure):
if not failure:
return
log.warning(failure.getErrorMessage)
print failure.getErrorMessage()
def get_clients_with_balance(self):
print >>sys.stderr, "Calling for clients with balance from server.."
return self.server.callRemote("postBalance").addCallback(self._got_clients_with_balance)
def get_clients_with_renewal(self):
print >>sys.stderr, "Calling for clients with renewals from server.."
return self.server.callRemote("postRenewals").addCallback(self._got_clients_with_renewal)
def get_clients_with_expiry(self):
print >>sys.stderr, "Calling for clients with expiry from server.."
return self.server.callRemote("postExpiry").addCallback(self._got_clients_with_expiry)
def get_all_clients(self):
print >>sys.stderr, "Calling for clients from server.."
return self.server.callRemote("postClients").addCallback(self._got_all_clients)
def get_company_details(self):
return self.server.callRemote("getCompanyDetails").addCallback(self._getCompanyDetails)
def _got_clients_with_balance(self, arg):
self.save_data(arg, "balance")
def _got_clients_with_renewal(self, arg):
self.save_data(arg, "renewal")
def _got_clients_with_expiry(self, arg):
self.save_data(arg, "expiry")
def _got_all_clients(self, arg):
self.save_data(arg, "allclients")
def _getCompanyDetails(self, arg):
self.save_data(arg, "compdetails")
def save_data(self, data, type_):
if isinstance(data, list) or isinstance(data, dict) or isinstance(data, tuple):
length = len(data)
elif isinstance(data, str):
data = Pickle.loads(data)
length = len(data)
else:
return
msg = "Saving downloaded data for %s with length %d" % (type_, length)
type_ = "bin/%s%s" % (type_, ".dat")
def save():
with open(type_, 'wb') as fl:
Pickle.dump(data, fl)
log.info(msg)
print >>sys.stderr, msg
save()
if __name__ == '__main__':
runKlass(['admin', 'passwd'])