python-3.x - Fluentd tcp 联系通过 netcat 工具成功。但是通过基本的 Python 3.7 失败了。为什么?

标签 python-3.x docker fluentd

所以我掀起了 基于 docker 的 fluentd TCP 日志收集器 .
按照此处的示例,https://docs.fluentd.org/input/tcp ,通过说成功地从我的主机 Win 10 WSL (Debian) 发送了一行

echo "my_service: 08:03:10 INFO [my_py_file:343]: My valuable log info." | netcat 127.0.0.1 5170
正如所希望的那样,这作为一个不错的 JSON 以 fluentd 的形式出现。但我想从 python 3.7 开始!所以:
import socket


def netcat(hn: str, p: int, content: bytes):
    """https://www.instructables.com/id/Netcat-in-Python/"""
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((hn, p))
    sock.sendall(content)
    sock.close()


msg_raw = "my_service: 08:03:10 INFO [my_py_file:343]: My valuable log info."

netcat('127.0.0.1', 5170, bytes(msg_raw, 'utf-8'))
WSL or no:这个 python 脚本运行,没有异常(exception)。 fluentd也没有任何 react ,我无法解释。 你们中的任何一个人都可以吗?
如果有任何后果:这是我的 fluentd.conf 中的相关部分.
<source>
  @type tcp
  @label mainstream
  @id pawc_tcp
  tag paws.tcp
  port 5170
  bind 0.0.0.0
  # https://docs.fluentd.org/parser/regexp
  <parse>
    @type regexp
    expression /^(?<service_uuid>[a-zA-Z0-9_-]+): (?<logtime>[^\s]+) (?<loglvl>[^\s]+) \[(?<file>[^\]:]+):(?<line>\d+)\]: (?<msg>.*)$/
    time_key logtime
    time_format %H:%M:%S
    types line:integer
  </parse>
</source>

<label mainstream>
  <match paws.tcp>
    @type file
    @id output_tcp
    path /fluentd/log/tcp.*.log
    symlink_path /fluentd/log/tcp.log
  </match>
</label>

最佳答案

尝试发送 \r\n\0在您的消息末尾。消息通过网络作为字节发送,因此它可能存储在缓冲区中,读取缓冲区的代码需要一种方法来知道消息已经结束。正则表达式也匹配行终止符,所以我认为那里也是必要的。

关于python-3.x - Fluentd tcp 联系通过 netcat 工具成功。但是通过基本的 Python 3.7 失败了。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63619754/

相关文章:

python-3.x - 如何更改 docker 容器的默认 python 版本?

docker - 在 Docker 容器内访问 Docker

oracle - sqlplus::ORA-01078:处理系统参数失败

Kubernetes:仅将 Fluentd 安装到命名空间

docker - 从由 kubernetes 管理的 docker 容器记录 nginx 事件

python-3.x - 对数据框的行进行排序

python-3.x - SVG 字形转 SVG/TTF/OTF 字体

python - 如何让 python 命令回到使用 python 2.7 Ubuntu?

linux - 是否可以在 docker 容器中运行 linux perf 工具

regex - 为TD代理编写正则表达式