简体   繁体   English

为什么从此Python脚本到此socat脚本的行缓冲管道不起作用?

[英]Why isn't line-buffered piping from this Python script into this socat script working?

I have a Python script that converts user IRC commands ("/nick hello", "/quit", etc.) into IRC protocol messages. 我有一个Python脚本,可将用户IRC命令(“ / nick hello”,“ / quit”等)转换为IRC协议消息。 It takes line by line input from stdin and outputs the translated message to stdout. 它从stdin逐行输入,并将转换后的消息输出到stdout。 I also have a very simple script based on socat that simply opens a TCP socket given an address and port. 我也有一个基于socat的非常简单的脚本,该脚本仅在给定地址和端口的情况下打开TCP套接字。 Both work fine on their own. 两者都可以正常工作。 But when I try to pipe the output of the IRC script into the socket script, nothing happens. 但是,当我尝试将IRC脚本的输出通过管道传输到套接字脚本时,什么也没发生。

This is what I want to happen (for clarity I added "<" for output and ">" for input): 这就是我发生的事情(为清楚起见,我在输出中添加了“ <”,在输入中添加了“>”):

$ user2irc | mksock 127.0.0.1 6667
< :irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
< :irc.aardei-6600K.local NOTICE * :*** Found your hostname (cached)
> /user aardbei
> /nick aardbei
< PING :1DE01324
> /pong 1DE01324
(...)

However, this is what actually happens: 但是,这是实际发生的情况:

$ user2irc | mksock 127.0.0.1 6667
< :irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
< :irc.aardei-6600K.local NOTICE * :*** Found your hostname (cached)
> /user aardbei
> /nick aardbei
(nothing happens)

This is what happens when I run the IRC script by itself, showing what it should be sending to the socket: 这是我自己运行IRC脚本并显示应发送到套接字的内容时发生的情况:

$ user2irc
> /user aardbei
< USER aardbei 8 * :aardbei
> /nick aardbei
< NICK :aardbei

And this is what happens when I run the socket script by itself, and typing in by hand the same IRC messages generated by the IRC script, showing that the IRC server does respond to them: 这就是当我自己运行套接字脚本并手动键入由IRC脚本生成的相同IRC消息(显示IRC服务器确实响应它们)时发生的情况:

$ mksock 127.0.0.1 6667
< :irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
< :irc.aardei-6600K.local NOTICE * :*** Found your hostname
> USER aardbei 8 * :aardbei
> NICK aardbei
< PING :862B79F4
> PONG :862B79F4
(...)

I tried piping the IRC messages into the command via echo, and it works just fine. 我尝试通过echo将IRC消息传递到命令中,并且效果很好。 You can see that the server sends back a PING message: 您可以看到服务器发回了PING消息:

$ echo -ne "USER aardbei 8 * :aardbei\r\nNICK :aardbei\r\n" | mksock 127.0.0.1 6667
:irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
:irc.aardei-6600K.local NOTICE * :*** Found your hostname (cached)
PING :FDB6F41D
ERROR :Closing Link: aardbei[127.0.0.1] (Read error)

I tried redirecting the output of the script to a file, and then reading it into the socket script, and that works too: 我尝试将脚本的输出重定向到文件,然后将其读入套接字脚本,这也可以工作:

$ user2irc > prgdump
/user aardbei
/nick aardbei
^D

$ cat prgdump
USER aardbei 8 * :aardbei
NICK :aardbei

$ mksock 127.0.0.1 6667 < prgdump
:irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
:irc.aardei-6600K.local NOTICE * :*** Found your hostname (cached)
PING :CEFB97C0
ERROR :Closing Link: aardbei[127.0.0.1] (Read error)

$ cat prgdump | mksock 127.0.0.1 6667
:irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
:irc.aardei-6600K.local NOTICE * :*** Found your hostname (cached)
PING :A74E64E4
ERROR :Closing Link: aardbei[127.0.0.1] (Read error)

I tried stdbuf -oL on both commands: 我在两个命令上都尝试了stdbuf -oL:

$ stdbuf -oL user2irc | stdbuf -oL mksock 127.0.0.1 6667
:irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
:irc.aardei-6600K.local NOTICE * :*** Found your hostname
/user aardbei
/nick aardbei
(nothing happens)

I tried piping it into cat: 我尝试将其管道输送到cat中:

$ user2irc | cat
/nick aardbei
(nothing happens)

I tried piping it into cat with explicit line-buffering: 我尝试使用显式行缓冲将其管道传输到cat中:

$ stdbuf -oL user2irc | stdbuf -oL cat
/nick aardbei
(nothing happens)

I tried piping cat into the socket script: 我尝试将cat管道到套接字脚本中:

$ cat | mksock 127.0.0.1 6667
< :irc.aardei-6600K.local NOTICE * :*** Looking up your hostname...
< :irc.aardei-6600K.local NOTICE * :*** Found your hostname (cached)
> USER aardbei 8 * :aardbei
> NICK aardbei
< PING :C2538D43

I tried using both bash and mksh. 我尝试同时使用bash和mksh。

Here are the contents of mksock : 以下是mksock的内容:

socat - TCP:$1:$2

Here are the contents of user2irc : http://pastebin.com/jSDdHEEF 这是user2irc的内容: http : //pastebin.com/jSDdHEEF

Why is it specifically this Python script that isn't line buffering when I pipe it into the socket script, and how can I make it so that it does? 当我将其通过管道传递给套接字脚本时,为什么它不是行缓冲的Python脚本,为什么要这样做呢?

Okay I fixed it by adding -u (for unbuffered) to the Python command on the first line of the script: 好的,我通过在脚本第一行的Python命令中添加-u(用于无缓冲)来修复它:

#!/bin/python3 -u

Now it works! 现在可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM