Completed
Push — master ( c12343...f97e79 )
by Andrii
14:30
created

Daemon.request()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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