InfluxDB:由于我的令牌和os.getenv,无法与客户联系
我只是从infuxdb开始。 在UI上,Python有一个部分可以从数据库开始,其中一个示例:初始化客户端,写数据,提出请求并关闭客户端。为了验证您必须在UI上生成令牌的请求,该请求允许保护请求。但是,当我运行示例代码时,我会从我的代码的第12行中获得错误“只能连接str(不是“非型”)到str。
代码:
from datetime import datetime
import os
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
# You can generate an API token from the "API Tokens Tab" in the UI
token = os.getenv("INFLUX_TOKEN")
org = "XXXXXXX"
bucket = "XXXXXXX Bucket"
with InfluxDBClient(url="https://europe-west1-1.gcp.cloud2.influxdata.com", token=token, org=org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
point = Point("mem") \
.tag("host", "host1") \
.field("used_percent", 23.43234543) \
.time(datetime.utcnow(), WritePrecision.NS)
write_api.write(bucket, org, point)
query = """from(bucket: "XXXXXXX Bucket") |> range(start: -1h)"""
tables = client.query_api().query(query, org=org)
for table in tables:
for record in table.records:
print(record)
client.close()
我知道问题来自OS.getEnv(“ influx_token”),因为它应该返回字符串,但实际上返回了非类型对象,但是我不知道为什么它不起作用。有了令牌,我得到了2件事:令牌的名称及其在创建时获得的代码。 I've tried :
- os.getenv("the name of the token")
- os.getenv("the code of the token")
- same of 2 above but with ' instead of "
All the time the same error, so please if someone可以帮我!
I am just starting with InfluxDB.
On the UI there is a section for Python to start with the database with an example: initialize the client, write data, make a request and close the client. In order to authenticate the request you have to generate a token on the UI which allows to secure the request. But when I run the example code I get the error "can only concatenate str (not "NoneType") to str" from line 12 of my code.
The code :
from datetime import datetime
import os
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
# You can generate an API token from the "API Tokens Tab" in the UI
token = os.getenv("INFLUX_TOKEN")
org = "XXXXXXX"
bucket = "XXXXXXX Bucket"
with InfluxDBClient(url="https://europe-west1-1.gcp.cloud2.influxdata.com", token=token, org=org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
point = Point("mem") \
.tag("host", "host1") \
.field("used_percent", 23.43234543) \
.time(datetime.utcnow(), WritePrecision.NS)
write_api.write(bucket, org, point)
query = """from(bucket: "XXXXXXX Bucket") |> range(start: -1h)"""
tables = client.query_api().query(query, org=org)
for table in tables:
for record in table.records:
print(record)
client.close()
I understand that the problem come from the os.getenv("INFLUX_TOKEN") because it's supposed to return a string but actually return NoneType object, but I dont know why it doesn't work. With the token I got 2 things : the name of the token and its code which I obtain when when it's created.
I've tried :
- os.getenv("the name of the token")
- os.getenv("the code of the token")
- same of 2 above but with ' instead of "
All the time the same error, so please if someone can help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的环境变量实际上有问题
os.getEnv(“ influx_token”)
返回无
这意味着influx_token
未设置变量。在生成令牌后手动将其设置在外壳中,
您可以通过输入SSH Shell
Export Influx_token =“您的influx-token”
您可以找到有关环境变量的更多信息。 https://stackoverflow.com/questions/71855049/Storing-sensistive-information-intheformation-in-the-code/71855256#71855256"> here
You actually have a problem with your environment variable if
os.getenv("INFLUX_TOKEN")
returnsNone
it means thatINFLUX_TOKEN
variable is not set.You can manually set it in your shell, after you generate the token by typing in your ssh shell
export INFLUX_TOKEN="your-influx-token"
You can find more about the environment variables here