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
@@ -13,8 +13,18 @@ class InfluxDBClient(object):
13
13
InfluxDB Client
14
14
"""
15
15
16
- def __init__ (self , host = 'localhost' , port = 8086 , username = 'root' ,
17
- password = 'root' , database = None , ssl = False , verify_ssl = False ):
16
+ def __init__ (
17
+ self ,
18
+ host = 'localhost' ,
19
+ port = 8086 ,
20
+ username = 'root' ,
21
+ password = 'root' ,
22
+ database = None ,
23
+ ssl = False ,
24
+ verify_ssl = False ,
25
+ use_udp = False ,
26
+ udp_port = 4444 ):
27
+
18
28
"""
19
29
Initialize client
20
30
"""
@@ -26,6 +36,11 @@ def __init__(self, host='localhost', port=8086, username='root',
26
36
27
37
self ._verify_ssl = verify_ssl
28
38
39
+ self .use_udp = use_udp
40
+ self .udp_port = udp_port
41
+ if use_udp :
42
+ self .udp_socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
43
+
29
44
self ._scheme = "http"
30
45
31
46
if ssl is True :
@@ -159,13 +174,16 @@ def write_points_with_precision(self, data, time_precision='s'):
159
174
'time_precision' : time_precision
160
175
}
161
176
162
- self .request (
163
- url = url ,
164
- method = 'POST' ,
165
- params = params ,
166
- data = data ,
167
- status_code = 200
168
- )
177
+ if self .use_udp :
178
+ self .send_packet (data )
179
+ else :
180
+ self .request (
181
+ url = url ,
182
+ method = 'POST' ,
183
+ params = params ,
184
+ data = data ,
185
+ status_code = 200
186
+ )
169
187
170
188
return True
171
189
@@ -604,3 +622,8 @@ def update_permission(self, username, json_body):
604
622
See also: src/api/http/api.go:l57
605
623
"""
606
624
raise NotImplementedError ()
625
+
626
+ def send_packet (self , packet ):
627
+ data = json .dumps (packet )
628
+ byte = data .encode ('utf-8' )
629
+ self .udp_socket .sendto (byte , (self ._host , self .udp_port ))
0 commit comments