gpt4 book ai didi

python-3.x - Python3插槽无效- Graphite

转载 作者:行者123 更新时间:2023-12-03 11:59:14 33 4
gpt4 key购买 nike

我创建了一个函数来将数据发送到 Graphite 服务器。它在执行时将度量名称,值和时间戳发送到 Graphite 服务器:

def collect_metric(metricname, value, timestamp):
sock = socket.socket()
sock.connect( ("localhost", 2003) )
sock.send("%s %s %s\n" % (metricname, value, timestamp))
sock.close()

上面的这个函数在Python2中运行良好。我不得不为Python3重写此函数。现在没有数据将被发送到 Graphite 。 Graphite /碳测井或其他方面没有测井记录...:
def collect_metric(metricname, value, timestamp):
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect( ("localhost", 2003) )
metricname = metricname.encode()
if type(value) == "str":
value = value.encode()
timestamp = timestamp.encode()
message = bytearray()
message = bytes(metricname+b" "+value+b" "+timestamp)
sock.sendall(message)
print(message.decode())
sock.close()

我没有收到任何错误。同样在终端上,我得到了正确的格式/输出(请参阅“print(message.decode())”)

有没有人知道为什么它不起作用?
谢谢。

最佳答案

字节数组没有任何编码。试试这个:message = (metricname+" "+value+" "+timestamp).encode("UTF-8")sock.send(messages)

关于python-3.x - Python3插槽无效- Graphite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655912/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com