Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP data sending from python to logstash fails

I am trying to send data from python script over TCP and receive the same in the logstash. But there is no exchange of data happening.

logstash.conf

input {
  tcp {
      port => 5959
      codec => json
  } 
}

filter{

}

output {
    stdout {codec => rubydebug}
}

python :

import json
import socket
import sys

HOST = "127.0.0.1"
PORT = 5959

try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
    sys.stderr.write("[ERROR] %s\n" % msg[1])
    sys.exit(1)

try:
    sock.connect((HOST, PORT))
except socket.error, msg:
    sys.stderr.write("[ERROR] %s\n" % msg[1])
    sys.exit(2)

msg = {'@message': 'python test message', '@tags': ['python', 'test']}

sock.sendall(json.dumps(msg))
sock.send('\n')
sock.close()
sys.exit(0)

After executing both the scripts I am neither getting any error nor data in the logstash so can someone help like what can be causing the issue.

like image 747
Raj Rajeshwar Singh Rathore Avatar asked Feb 24 '26 12:02

Raj Rajeshwar Singh Rathore


1 Answers

I just added sock.send('\n') after sock.sendall(json.dumps(msg)) and it worked. It seems that there is an open issue with logstash-codec-json as1 :

The logstash 'json' plugin still requires a newline '\n' to terminate json logs being sent over a TCP input.2

References :

  1. TCP data sending from pyton to logstash fails.

  2. The logstash 'json' codec still requires a newline '\n' as a delimiter to terminate json logs being sent over a TCP input.

like image 55
Raj Rajeshwar Singh Rathore Avatar answered Feb 27 '26 01:02

Raj Rajeshwar Singh Rathore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!