-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.py
executable file
·63 lines (53 loc) · 1.55 KB
/
connection.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
import ispapi.util
import urllib
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
from ispapi.response import Response
"""
ISPAPI Connection
"""
class Connection:
def __init__(self, config):
"""
Constructor
"""
self._config = config
def call_raw_http(self, command, config = None):
"""
Make a curl API call over HTTP(S) and returns the response as a string
"""
post = {}
if ('login' in self._config):
post['s_login'] = self._config['login']
if ('password' in self._config):
post['s_pw'] = self._config['password']
if ('entity' in self._config):
post['s_entity'] = self._config['entity']
if ('user' in self._config):
post['s_user'] = self._config['user']
if ('role' in self._config):
post['s_login'] = self._config['login'] + "!" + self._config['role']
post['s_command'] = ispapi.util.command_encode(command)
try:
# For Python 3.0 and later
post = urllib.parse.urlencode(post)
except:
# Fall back to Python 2's urllib2
post = urllib.urlencode(post)
response = urlopen(self._config['url'], post.encode('UTF-8'))
content = response.read()
return content
def call_raw(self, command, config = None):
"""
Make a curl API call and returns the response as a string
"""
return self.call_raw_http(command, config)
def call(self, command, config = None):
"""
Make a curl API call and returns the response as a response object
"""
return Response(self.call_raw(command, config))