From 9d2a290c78f41ff96e72b85f07ce1e5678487d08 Mon Sep 17 00:00:00 2001 From: aviau Date: Fri, 6 Jun 2014 16:07:51 -0400 Subject: [PATCH 1/2] Added UDP support --- influxdb/client.py | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 8372846d..a7c1c740 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -3,7 +3,7 @@ python client for influxdb """ import json - +import socket import requests session = requests.Session() @@ -13,8 +13,18 @@ class InfluxDBClient(object): InfluxDB Client """ - def __init__(self, host='localhost', port=8086, username='root', - password='root', database=None, ssl=False, verify_ssl=False): + def __init__( + self, + host='localhost', + port=8086, + username='root', + password='root', + database=None, + ssl=False, + verify_ssl=False, + use_udp=False, + udp_port=4444): + """ Initialize client """ @@ -26,6 +36,11 @@ def __init__(self, host='localhost', port=8086, username='root', self._verify_ssl = verify_ssl + self.use_udp = use_udp + self.udp_port = udp_port + if use_udp: + self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self._scheme = "http" if ssl is True: @@ -159,13 +174,16 @@ def write_points_with_precision(self, data, time_precision='s'): 'time_precision': time_precision } - self.request( - url=url, - method='POST', - params=params, - data=data, - status_code=200 - ) + if self.use_udp: + self.send_packet(data) + else: + self.request( + url=url, + method='POST', + params=params, + data=data, + status_code=200 + ) return True @@ -604,3 +622,8 @@ def update_permission(self, username, json_body): See also: src/api/http/api.go:l57 """ raise NotImplementedError() + + def send_packet(self, packet): + data = json.dumps(packet) + byte = data.encode('utf-8') + self.udp_socket.sendto(byte, (self._host, self.udp_port)) From bc90e455a88a6c3e5576531a11e5fb3f8c4619a4 Mon Sep 17 00:00:00 2001 From: aviau Date: Fri, 6 Jun 2014 16:44:26 -0400 Subject: [PATCH 2/2] Added test_write_points_udp --- tests/influxdb/client_test.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/influxdb/client_test.py b/tests/influxdb/client_test.py index e8601441..f3667d43 100644 --- a/tests/influxdb/client_test.py +++ b/tests/influxdb/client_test.py @@ -4,6 +4,7 @@ """ import json import requests +import socket from nose.tools import raises from mock import patch @@ -36,7 +37,7 @@ def request(*args, **kwargs): assert isinstance(data, str) # Data must be a JSON string - assert c == json.loads(data) + assert c == json.loads(data, strict=True) c = data @@ -91,6 +92,27 @@ def test_write_points(self): cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') assert cli.write_points(data) is True + def test_write_points_udp(self): + data = [ + { + "points": [ + ["1", 1, 1.0], + ["2", 2, 2.0] + ], + "name": "foo", + "columns": ["column_one", "column_two", "column_three"] + } + ] + + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.bind(('0.0.0.0', 4444)) + + cli = InfluxDBClient('localhost', 8086, 'root', 'root', 'test', use_udp=True, udp_port=4444) + cli.write_points(data) + + received_data, addr = s.recvfrom(1024) + assert data == json.loads(received_data.decode(), strict=True) + @raises(Exception) def test_write_points_fails(self): with _mocked_session('post', 500) as mocked: @@ -273,4 +295,4 @@ def test_delete_database_user(self): @raises(NotImplementedError) def test_update_permission(self): cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') - cli.update_permission('admin', []) + cli.update_permission('admin', []) \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy