Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Example for sending information via udp needed in documentation #646

Closed
@shantanoo-desai

Description

@shantanoo-desai

It would be really helpful to perhaps setup a udp example in the repository. There is just mentions of it here and there in the API documents and they are not clear for usage.

  1. Apparently the docs for send_packet() should mention a list instead of sequence which is very confusing.

  2. if the protocol is json it mentions the value should be passed as a dict. For starters, I just copied the example json_body in the code and this is the Error I get:

Traceback (most recent call last):
  File "gpsinflux-udp.py", line 25, in <module>
    main()
  File "gpsinflux-udp.py", line 21, in main
    client.send_packet(json_body)
  File "/usr/local/lib/python3.4/dist-packages/influxdb/client.py", line 877, in send_packet
    data = make_lines(packet).encode('utf-8')
  File "/usr/local/lib/python3.4/dist-packages/influxdb/line_protocol.py", line 119, in make_lines
    static_tags = data.get('tags')
AttributeError: 'list' object has no attribute 'get'
  1. If I only pass the json_body as a dict I get:
Traceback (most recent call last):
  File "gpsinflux-udp.py", line 23, in <module>
    main()
  File "gpsinflux-udp.py", line 19, in main
    client.send_packet(json_body)
  File "/usr/local/lib/python3.4/dist-packages/influxdb/client.py", line 877, in send_packet
    data = make_lines(packet).encode('utf-8')
  File "/usr/local/lib/python3.4/dist-packages/influxdb/line_protocol.py", line 120, in make_lines
    for point in data['points']:
KeyError: 'points'

Code

case 2

from influxdb import InfluxDBClient

def main():
    client = InfluxDBClient('localhost', 8089, use_udp=True, udp_port=8089,
    database='example')

    json_body = [{
        "measurement": "cpu_load_short",
        "tags": {
            "host": "server01",
            "region": "us-west"
        },
        "time": "2009-11-10T23:00:00Z",
        "fields": {
            "value": 0.64
        }
    }]

    client.send_packet(json_body)


if __name__ == '__main__':
    main()

case 3

from influxdb import InfluxDBClient

def main():
    client = InfluxDBClient('localhost', 8089, use_udp=True, udp_port=8089,
    database='example')

    json_body = {
        "measurement": "cpu_load_short",
        "tags": {
            "host": "server01",
            "region": "us-west"
        },
        "time": "2009-11-10T23:00:00Z",
        "fields": {
            "value": 0.64
        }
    }

    client.send_packet(json_body)


if __name__ == '__main__':
    main()

Won't harm to add a simple udp example for the repo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions