简体   繁体   English

如何使用python发送nc类似命令?

[英]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:150statsD正在获得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.

相关问题 如何使用 Python 将 .mat 文件转换为 netcdf 文件(.nc)? - How to convert .mat file into netcdf file (.nc) using Python? 如何使用 AJAX 或类似工具将 HTML 表单数据发送到 Python 服务器? - How to send HTML form data to Python server using AJAX or similar? 如何在python中使用paramiko将输入发送到命令? - How to send inputs to a command using paramiko in python? 尝试获取“nc -vz”的输出和返回代码<host><port> &quot; 在 Python3 中使用 subprocess.Popen 命令 - trying to get the output and return code for "nc -vz <host> <port>" command using subprocess.Popen in Python3 使用 python 读取 .nc (netcdf) 文件 - Read .nc (netcdf) files using python 如何使用 python xarray 从单个 NetCDF .nc 文件导入多个表? - How do I import multiple tables from a single NetCDF .nc file using python xarray? 如何使用python在命令提示符下用户名后发送密码 - How to send the password after user name in command prompt using python 如何在 Android 上使用 Python 通过蓝牙发送 ESC 打印命令? - How to send a ESC printing command over Bluetooth using Python on Android? 如何使用QCMDEXC从Python发送iSeries命令 - How to send iSeries command from Python using QCMDEXC 如何使用Python子进程发送“复制”命令 - How Can I Send a “Copy” Command Using Python Subprocess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM