Python sample code

Trying to run the sample python example to insert data into influx and I receive The following error.
reate database: example
Traceback (most recent call last):
File “insert.py”, line 65, in
main(host=args.host, port=args.port)
File “insert.py”, line 30, in main
client.create_database(dbname)
File “/usr/lib/python2.7/site-packages/influxdb/client.py”, line 488, in create_database
self.query(“CREATE DATABASE “%s”” % dbname)
File “/usr/lib/python2.7/site-packages/influxdb/client.py”, line 339, in query
expected_response_code=expected_response_code
File “/usr/lib/python2.7/site-packages/influxdb/client.py”, line 246, in request
raise e
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:581)

I have installed all packages. pyopenssl is installed.

Any suggestions.

@carl Hard to debug when I don’t know what the code looks like. Can you post the script here?

Hi Jack - sure - its really the sample provide by influx, I just want to make sure I could connect.

import argparse

from influxdb import InfluxDBClient

def main(host=‘localhost’, port=8086):
user = 'root’
password = 'root’
dbname = 'example’
dbuser = 'smly’
dbuser_password = 'my_secret_password’
query = 'select value from cpu_load_short;'
json_body = [
{
“measurement”: “cpu_load_short”,
“tags”: {
“host”: “server01”,
“region”: “us-west”
},
“time”: “2009-11-10T23:00:00Z”,
“fields”: {
“value”: 0.64
}
}
]

client = InfluxDBClient(host, port, user, password, dbname, verify_ssl=False)

print("Create database: " + dbname)
client.create_database(dbname)

print("Create a retention policy")
client.create_retention_policy('awesome_policy', '3d', 3, default=True)

print("Switch user: " + dbuser)
client.switch_user(dbuser, dbuser_password)

print("Write points: {0}".format(json_body))
client.write_points(json_body)

print("Queying data: " + query)
result = client.query(query)

print("Result: {0}".format(result))

print("Switch user: " + user)
client.switch_user(user, password)

print("Drop database: " + dbname)
client.drop_database(dbname)

def parse_args():
parser = argparse.ArgumentParser(
description=‘example code to play with InfluxDB’)
parser.add_argument(’–host’, type=str, required=False, default=‘localhost’,
help=‘hostname of InfluxDB http API’)
parser.add_argument(’–port’, type=int, required=False, default=8086,
help=‘port of InfluxDB http API’)
return parser.parse_args()

if name == ‘main’:
args = parse_args()
main(host=args.host, port=args.port)

@carl Are you passing in the -port and -host args when invoking the script?

yes -

i’m specifying --host and --port

Jack - any ideas? what the configuration issue could be? Thanks, Carl

@carl it looks like the script is failing at the first call to the database. Are you running an influxd instance at localhost:8086?

Hey Jack - you are correct on both counts. I can connect to influxd using influx. python crashes. running influxd and python on the say box.

Jack - do you have any suggestions.

Does anyone know if there are software version incompatibilities?

@carl I dont think this is a bug with InfluxDB. It looks like your python has a syntax error. I have this code running:

import argparse

from influxdb import InfluxDBClient

def main(host='localhost', port=8086):
    user = 'root'
    password = 'root'
    dbname = 'example'
    dbuser = 'smly'
    dbuser_password = 'my_secret_password'
    query = 'select value from cpu_load_short;'
    json_body = [
        {
            "measurement": "cpu_load_short",
            "time": "2009-11-10T23:00:00Z",
            "tags": {
                "host": "server01",
                "region": "us-west"
            },
            "fields": {
                "value": 0.64
            }
        }
    ]
    client = InfluxDBClient(host, port, user, password, dbname, verify_ssl=False)

    print("Create database: " + dbname)
    client.create_database(dbname)

    print("Create a retention policy")
    client.create_retention_policy('awesome_policy', '3d', 3, default=True)

    print("Switch user: " + dbuser)
    client.switch_user(dbuser, dbuser_password)

    print("Write points: {0}".format(json_body))
    client.write_points(json_body)

    print("Queying data: " + query)
    result = client.query(query)

    print("Result: {0}".format(result))

    print("Switch user: " + user)
    client.switch_user(user, password)

    print("Drop database: " + dbname)
    client.drop_database(dbname)

def parse_args():
    parser = argparse.ArgumentParser(description='example code to play with InfluxDB')
    parser.add_argument('--host', type=str, required=False, default='localhost', help='hostname of InfluxDB http API')
    parser.add_argument('--port', type=int, required=False, default=8086, help='port of InfluxDB http API')
    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()
    print args
    main(host=args.host, port=args.port)

Hi Jack -

I did a diff on original code I download from influx and the code you attached - aside from a few indentation differences code is the same. I ran your copy on my box, I get the same ssl error.

Must be a library or package version causing the problem. i’m running on a SUSE box any incompatibilities you know of.

Very strange as both client python and server are running local.

Hi Jack -

The problem was not the code, yours or mine. The problem was caused by a network issue the server was configured to go thru a proxy. I reconfigured the proxy to say local - worked fine.

Sorry for not noticing the configuration before asking for help.

Thanks for your time.

Regards,
Carl

@carl Glad you were able to get that fixed!