3
3
python client for influxdb
4
4
"""
5
5
import json
6
-
6
+ import socket
7
7
import requests
8
8
session = requests .Session ()
9
9
@@ -12,14 +12,23 @@ class InfluxDBClientError(Exception):
12
12
def __init__ (self , message , code ):
13
13
self .message = message
14
14
self .code = code
15
-
15
+
16
16
class InfluxDBClient (object ):
17
17
"""
18
18
InfluxDB Client
19
19
"""
20
20
21
- def __init__ (self , host = 'localhost' , port = 8086 , username = 'root' ,
22
- password = 'root' , database = None , ssl = False , verify_ssl = False , timeout = 0 ):
21
+ def __init__ (self ,
22
+ host = 'localhost' ,
23
+ port = 8086 ,
24
+ username = 'root' ,
25
+ password = 'root' ,
26
+ database = None ,
27
+ ssl = False ,
28
+ verify_ssl = False ,
29
+ timeout = 0 ,
30
+ use_udp = False ,
31
+ udp_port = 4444 ):
23
32
"""
24
33
Initialize client
25
34
"""
@@ -32,6 +41,11 @@ def __init__(self, host='localhost', port=8086, username='root',
32
41
33
42
self ._verify_ssl = verify_ssl
34
43
44
+ self .use_udp = use_udp
45
+ self .udp_port = udp_port
46
+ if use_udp :
47
+ self .udp_socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
48
+
35
49
self ._scheme = "http"
36
50
37
51
if ssl is True :
@@ -102,7 +116,7 @@ def request(self, url, method='GET', params=None, data=None,
102
116
103
117
if response .status_code == status_code :
104
118
return response
105
-
119
+
106
120
else :
107
121
error = InfluxDBClientError ("{0}: {1}" .format (response .status_code , response .content ), response .status_code )
108
122
raise error
@@ -167,13 +181,16 @@ def write_points_with_precision(self, data, time_precision='s'):
167
181
'time_precision' : time_precision
168
182
}
169
183
170
- self .request (
171
- url = url ,
172
- method = 'POST' ,
173
- params = params ,
174
- data = data ,
175
- status_code = 200
176
- )
184
+ if self .use_udp :
185
+ self .send_packet (data )
186
+ else :
187
+ self .request (
188
+ url = url ,
189
+ method = 'POST' ,
190
+ params = params ,
191
+ data = data ,
192
+ status_code = 200
193
+ )
177
194
178
195
return True
179
196
@@ -266,7 +283,7 @@ def query(self, query, time_precision='s', chunked=False):
266
283
try :
267
284
res = json .loads (response .content )
268
285
except TypeError :
269
- # must decode in python 3
286
+ # must decode in python 3
270
287
res = json .loads (response .content .decode ('utf8' ))
271
288
272
289
return res
@@ -618,3 +635,8 @@ def update_permission(self, username, json_body):
618
635
See also: src/api/http/api.go:l57
619
636
"""
620
637
raise NotImplementedError ()
638
+
639
+ def send_packet (self , packet ):
640
+ data = json .dumps (packet )
641
+ byte = data .encode ('utf-8' )
642
+ self .udp_socket .sendto (byte , (self ._host , self .udp_port ))
0 commit comments