# How to use Python to send data to logstash

**URL:** https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118
**Category:** Logstash
**Created:** [March 11, 2016, 7:18am UTC](https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118 "2016-03-11T07:18:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![szcountryboy](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@szcountryboy](https://discuss.elastic.co/u/szcountryboy)
#### Post date: [March 11, 2016, 7:18am UTC](https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118/1 "2016-03-11T07:18:04Z")

</div>

I use Python2.7 send data to the logstash above, the use of ordinary TCP port data transmission.

> msgDict["@metadata"] = {  
> "beat":"python-test",  
> "type":"python"  
> }  
> msgDict["@timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%S",now)  
> msgDict["tags"] = ["python","192.168.4.15"]  
> msgDict["type"] = "python"

> tcpsock = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM)  
> server\_address = ("127.0.0.1", 5044)  
> tcpsock.connect(server\_address)  
> msg = dumps(msgDict) + "\n"  
> tcpsock.sendall(msg)  
> tcpsock.close()

But logstash accepted after the show format is not correct, what is  
the reason for the formation of.Lumberjack\_formatter-0.1.4 also  
installed

> [33mBeats Input: Remote connection closed {:peer=\>"192.168.59.198:64911", :exception=\>#Lumberjack::Beats::Connection::ConnectionClosed: Lumberjack::Beats::Connection::ConnectionClosed wrapping: Lumberjack::Beats::Parser::UnsupportedProtocol, unsupported protocol 123\>, :level=\>:warn}

> [33mBeats Input: Remote connection closed {:peer=\>"127.0.0.1:56536", :exception=\>#Lumberjack::Beats::Connection::ConnectionClosed: Lumberjack::Beats::Connection::ConnectionClosed wrapping: Lumberjack::Beats::Parser::UnsupportedProtocol, unsupported protocol 123\>, :level=\>:warn}

> [33mBeats Input: Remote connection closed {:peer=\>"127.0.0.1:58986", :exception=\>#Lumberjack::Beats::Connection::ConnectionClosed: Lumberjack::Beats::Connection::ConnectionClosed wrapping: Lumberjack::Beats::Parser::UnsupportedProtocol, unsupported protocol 39\>, :level=\>:warn}

logstash configure file content  
input {  
beats {  
port =\> 5044  
}  
}

Logstash both to accept the beat source data, but also want to let him accept user defined data

Thanks

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 11, 2016, 8:26am UTC](https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118/2 "2016-03-11T08:26:36Z")

</div>

Don't use a beats input, use a tcp input (or a udp input if you prefer to send the logs over UDP).

---

<div class="post-metadata">

### Author: ![szcountryboy](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@szcountryboy](https://discuss.elastic.co/u/szcountryboy)
#### Post date: [March 11, 2016, 8:47am UTC](https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118/3 "2016-03-11T08:47:20Z")

</div>

Thank you very much for your answer.

If you use tcp/udp, then send the past data as a field (message), or to create multiple fields in logstash filter. I want to send according to the content, generating a plurality of fields, similar to (topbeat),

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 11, 2016, 8:53am UTC](https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118/4 "2016-03-11T08:53:21Z")

</div>

Sure. Send the log message as JSON (like you attempted to above) and use a json codec on the receiving end.

```auto
input {
  tcp {
    ...
    codec => json
  }
}

```

---

<div class="post-metadata">

### Author: ![szcountryboy](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@szcountryboy](https://discuss.elastic.co/u/szcountryboy)
#### Post date: [March 11, 2016, 10:00am UTC](https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118/5 "2016-03-11T10:00:04Z")

</div>

Thanks very much

input {  
tcp {  
port =\> 5040  
codec =\> json  
}  
beats {  
port =\> 5044  
}  
}

---

<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: [July 6, 2017, 5:07am UTC](https://discuss.elastic.co/t/how-to-use-python-to-send-data-to-logstash/44118/6 "2017-07-06T05:07:24Z")

</div>


