# TCP data sending from pyton to logstash fails

**URL:** https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358
**Category:** Logstash
**Created:** [May 3, 2018, 4:27am UTC](https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358 "2018-05-03T04:27:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![RRSR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rrsr/32/27766_2.png) [@RRSR](https://discuss.elastic.co/u/RRSR)
#### Post date: [May 3, 2018, 4:27am UTC](https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358/1 "2018-05-03T04:27:02Z")

</div>

Hi,  
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 socket
import json
import sys

HOST = "127.0.0.1"
PORT = 5959

print "#STARTED#"

try:
  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])
    sys.exit(1)

try:
  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])
    sys.exit(2)

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

sock.sendall(json.dumps(msg))
print "end"
sock.close()
sys.exit(0)

```

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.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [May 3, 2018, 4:43am UTC](https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358/2 "2018-05-03T04:43:32Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![RRSR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rrsr/32/27766_2.png) [@RRSR](https://discuss.elastic.co/u/RRSR)
#### Post date: [May 3, 2018, 4:48am UTC](https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358/3 "2018-05-03T04:48:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [May 3, 2018, 5:07am UTC](https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358/4 "2018-05-03T05:07:10Z")

</div>

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](https://github.com/logstash-plugins/logstash-codec-json/issues/15) around this.

---

<div class="post-metadata">

### Author: ![RRSR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rrsr/32/27766_2.png) [@RRSR](https://discuss.elastic.co/u/RRSR)
#### Post date: [May 3, 2018, 5:44am UTC](https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358/5 "2018-05-03T05:44:04Z")

</div>

Thanks for the quick reply and explanation.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 31, 2018, 5:44am UTC](https://discuss.elastic.co/t/tcp-data-sending-from-pyton-to-logstash-fails/130358/6 "2018-05-31T05:44:04Z")

</div>

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