Completed
Push — master ( b34b71...1c0f48 )
by Andrii
12:24
created

Daemon.start()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 1
c 4
b 0
f 1
dl 0
loc 3
rs 10
1
import os
2
import sys
3
import time
4
import socket
5
6
from pprint import pprint
7
8
from heppy.EPP import REPP
9
from heppy.Error import Error
10
from heppy.Login import Login
11
from heppy.Client import Client
12
from heppy.Request import Request
13
from heppy.Response import Response
14
15
class Daemon:
16
    def __init__(self, config):
17
        self.config = config
18
        self.is_external = False
19
        self.client = None
20
21
    def start(self,args = {}):
22
        self.connect()
23
        self.login(args)
24
25
    def connect(self):
26
        if self.client is not None:
27
            return
28
        if self.is_external:
29
            self.connect_external()
30
        else:
31
            self.connect_internal()
32
33
    def login(self, args = {}):
34
        greeting = self.client.get_greeting()
35
        greetobj = Response.parsexml(greeting)
36
        pprint(greetobj.data)
37
        try:
38
            request = Login.build(self.config, greeting, args)
39
            query = str(request)
40
            print Request.prettifyxml(query)
41
            reply = self.client.request(query)
42
            print Request.prettifyxml(reply)
43
        except Error as e:
44
            Error.die(2, 'failed perform login request')
45
        error = None
46
        try:
47
            response = Response.parsexml(reply)
48
            data = response.data
49
            pprint(data)
50
        except Error as e:
51
            error = e.message
52
            data = e.data
53
        if error is not None and data['resultCode']!='2002':
54
            Error.die(2, 'bad login response', data)
55
        print 'LOGIN OK'
56
57
    def connect_internal(self):
58
        self.client = REPP(self.config['epp'])
59
60
    def connect_external(self):
61
        try:
62
            self.client = Client(self.config['local']['address'])
63
            self.client.connect()
64
        except socket.error as e:
65
            os.system(self.config['zdir'] + '/eppyd ' + self.config['path'] + ' &')
66
            time.sleep(2)
67
            self.client = Client(self.config['local']['address'])
68
69
    def stop(args = {}):
70
        Error.die(3, 'failed stop', config)
71
72