[英]How to send nc similar command using python?
I am trying to send parameter foo
and its value to statsD using python (python-2.7) program. 我正在尝试使用python(python-2.7)程序将参数foo
及其值发送到statsD 。 When I send packets from command line, using below netcat command, statsD gets value for foo:150 which is correct. 当我从命令行使用以下netcat命令发送数据包时,statsD获取foo:150的值,这是正确的。 echo "foo:150|c" | nc -u -w0 127.0.0.1 8125
However, when I send foo:150
through Python program, statsD
is getting large random values for foo
, instead of 150
. 但是,当我通过Python程序发送foo:150
, statsD
正在获得foo
大随机值,而不是150
。 Python program and statsD
receive value output is below. Python程序和statsD
接收值输出如下。 Any idea what is error in python program that is causing statsD
to receive random value, but working perfectly when sending through nc (netcat) command? 知道导致statsD
接收随机值但在通过nc(netcat)命令发送时能正常工作的python程序中的错误是什么吗?
PYTHON PROGRAM: PYTHON计划:
import socket
import random
UDP_IP = "127.0.0.1"
UDP_PORT = 8125
while(True):
MESSAGE = "foo:150|c"
#print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StatsD received value: StatsD收到的值:
timer_data: {},
counter_rates:
{ 'statsd.bad_lines_seen': 0,
'statsd.packets_received': 19828,
'statsd.metrics_received': 19828,
foo: 2974200 },
sets: {},
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I figured out the answer. 我想出了答案。 Seems like when I changed sending value from counter to as gauges, I am getting correct value. 好像当我将发送值从计数器更改为仪表时,我正在获取正确的值。 So, I think problem was not in my python program, but it is some logic in statsD
that was giving me unexpected random values for foo
when passing as part of counter. 因此,我认为问题不在我的python程序中,而是statsD
中的某些逻辑在作为计数器的一部分传递时给了我意想不到的foo
随机值。
So in short, when I did change from MESSAGE = "foo:150|c"
to MESSAGE = "foo:150|g"
, I start getting correct value for foo
. 简而言之,当我从MESSAGE = "foo:150|c"
更改为MESSAGE = "foo:150|g"
,我开始为foo
获取正确的值。 Below is the output: 以下是输出:
{ counters:
{ 'statsd.bad_lines_seen': 0,
'statsd.packets_received': 150383,
'statsd.metrics_received': 150383 },
timers: {},
gauges: { foo: 150, 'statsd.timestamp_lag': 0 },
timer_data: {},
counter_rates:
{ 'statsd.bad_lines_seen': 0,
'statsd.packets_received': 150383,
'statsd.metrics_received': 150383 },
sets: {},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.