将Python输出管道传输到NetCat-time.sleep()中断输出?

时间:2019-05-16 16:08:33

标签: python python-3.x windows-subsystem-for-linux netcat

我正在尝试在Windows上使用WSL将python脚本的输出转储到netcat。在一个终端中,我将我的python程序和netcat设置为侦听器模式。在另一个终端中,我正在连接到此侦听器,并期望看到我的python输出。下面的代码有效,我能够成功完成此操作,但是在函数调用之间添加time.sleep(1)时,我没有收到任何Python输出。为什么会这样?

我尝试使用print()函数代替sys.stdout.write()。我尝试了不同的nc配置。

Python生产者脚本:

import time
from sys import stdout

def generate_stream_data():
    stdout.write('some text\n')

if __name__ == '__main__':
    iterations = 10000
    while iterations > 0:
        generate_stream_data()
        time.sleep(1)  # this causes nothing to get piped to nc

在WSL中输入python输出:

python above_code.py | nc -lk localhost 9999

在另一个WSL终端中:

nc -v localhost 9999

注释time.sleep(1)可以让我在第二个终端中看到输出。我已经测试了独立的Python程序,该程序成功打印出“ some_ text”,并以1秒的延迟进行了控制台。为什么加上这个会导致我在netcat中看不到任何内容?

0 个答案:

没有答案