-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.py
138 lines (103 loc) · 3.67 KB
/
request.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
import httplib
import base64
class HTTPStatusException(Exception):
def __init__(self, status, message):
self.status = status
self.message = message
def __str__(self):
return "Error [%s]: %s" % (self.status, self.message)
class HttpRequest(object):
def __init__(self, server, username, password, port=8123, ssl=False):
self.server = server
self.port = port
self.username = username
self.password = password
self.ssl = ssl
def _get_connection(self):
if (self.ssl):
conn = httplib.HTTPSConnection(self.server, self.port)
else:
conn = httplib.HTTPConnection(self.server, self.port)
return conn
def delete(self, resource):
conn = self._get_connection()
conn.putrequest("DELETE", resource)
self._set_auth_header(conn)
conn.endheaders()
resp = conn.getresponse()
content = resp.read()
headers = {}
raw_headers = resp.getheaders()
for header in raw_headers:
header_name = header[0]
header_value = header[1]
headers[header_name] = header_value
results = { "headers": headers,
"content": content,
"code": resp.status
}
return results
def get(self, resource):
conn = self._get_connection()
conn.putrequest("GET", resource)
self._set_auth_header(conn)
conn.endheaders()
resp = conn.getresponse()
content = resp.read()
raw_headers = resp.getheaders()
for header in raw_headers:
header_name = header[0]
header_value = header[1]
headers = {}
headers[header_name] = header_value
results = { "headers": headers,
"content": content,
"code": resp.status
}
return results
def put(self, resource, data):
conn = self._get_connection()
conn.putrequest("PUT", resource)
self._set_auth_header(conn)
conn.putheader("Content-Length", "%d" % len(data))
conn.endheaders()
conn.send(data)
resp = conn.getresponse()
raw_headers = resp.getheaders()
content = resp.read()
resp_headers = {}
for header in raw_headers:
header_name = header[0]
header_value = header[1]
resp_headers[header_name] = header_value
results = { "headers": resp_headers,
"content": content,
"code": resp.status
}
return results
def post(self, resource, data):
conn = self._get_connection()
conn.putrequest("POST", resource)
self._set_auth_header(conn)
conn.putheader("Content-Length", "%d" % len(data))
conn.endheaders()
conn.send(data)
resp = conn.getresponse()
raw_headers = resp.getheaders()
content = resp.read()
resp_headers = {}
for header in raw_headers:
header_name = header[0]
header_value = header[1]
resp_headers[header_name] = header_value
results = { "headers": resp_headers,
"content": content,
"code": resp.status
}
return results
def _set_auth_header(self, connection):
# We don't use the base64.encodestring() method here becuase it automatically adds
# newlines
base64string = "Basic " + base64.b64encode('%s:%s' % (self.username, self.password))
connection.putheader("Authorization", base64string)