TCP data sending from pyton 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.


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


output {
    stdout {codec => rubydebug}

python :

import socket
import json
import sys

HOST = ""
PORT = 5959

print "#STARTED#"

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

  sock.connect((HOST, PORT))
  print "socket connected to HOST: "+HOST+" PORT: "+str(PORT)
  print "socket connected. sock: " + str(sock)
except socket.error, msg:
    print "error"
    sys.stderr.write("[ERROR] %s\n" % msg[1])

msg = {'@message': 'python test message', '@tags': ['python', 'test']}
print "sending message: " + str(msg)

print "end"

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

I believe you need to send a new line after the JSON dump. Can you try adding one to see if that makes a difference?

1 Like

Yeah, that worked. I just added sock.send('\n') after sock.sendall(json.dumps(msg)).
Can you explain why this new line is needed?

Given that the json_lines codec exists, it is reasonable to expect the json codec should be able to handle this scenario. It seems like there is an open issue around this.

1 Like

Thanks for the quick reply and explanation.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.