简体   繁体   中英

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. It takes line by line input from stdin and outputs the translated message to stdout. I also have a very simple script based on socat that simply opens a TCP socket given an address and port. Both work fine on their own. But when I try to pipe the output of the IRC script into the socket script, nothing happens.

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:

$ 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:

$ 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. You can see that the server sends back a PING message:

$ 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 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:

$ user2irc | cat
/nick aardbei
(nothing happens)

I tried piping it into cat with explicit line-buffering:

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

I tried piping cat into the socket script:

$ 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.

Here are the contents of mksock :

socat - TCP:$1:$2

Here are the contents of 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?

Okay I fixed it by adding -u (for unbuffered) to the Python command on the first line of the script:

#!/bin/python3 -u

Now it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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