Issues with sending lines over netcat to Python
<p>I am piping a program's stdout to netcat: <code>nc -u localhost 50000</code>.  Listening on UDP 50000 is a Python program that does something like this:</p>

<pre><code>  lstsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  lstsock.setblocking(0)

  while True:
        print '1'
        try:
            tmp = lstsock.recv(SOCK_BUFSZ)
        except socket.error, e:
            if e.args[0] == errno.EWOULDBLOCK:
                sleep(.5)
                continue
            else:
                print("Socket error: {}".format(e))
                return
        print tmp
</code></pre>

<p>I'll always get a few lines, but then the program hangs on <code>print '1'</code>.  When I run the line-generating program, the output is a line to stdin about every second.  What's going on here?</p>

<p>Edit, in case it's somehow related: The program producing lines is in docker (run with <code>--net="host"</code>, and the server (accepting lines) is on the host running docker.  Docker is sending it over 127.0.0.1.</p>

<p>Another edit: It seems to stop accepting input when SOCK_BUFSZ bytes were received.  It's not recycling the buffer?</p>

<p>Update: this seems to be an issue with Docker.  It works on localhost, but not from the container.  I have connectivity (I can ping the server, and the first burst of data gets through).</p>

<p>This Python script worked for me:</p>

<pre><code>import socket

lstsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
lstsock.bind(('127.0.0.1', 50000))

SOCK_BUFSZ = 4096
counter = 1
while True:
    print "counter = %s" % counter
    counter += 1
    data, addr = lstsock.recvfrom(SOCK_BUFSZ)
    print data
</code></pre>

<p>I used this bash script to send lines:</p>

<pre><code>while true ;
do
    echo "Running..."
    echo -n "hello" | nc -w 0 -u "127.0.0.1" 50000
    sleep 1
done
</code></pre>

<p>I didn't see the point of setting the socket to be non-blocking when time was wasted in a sleep.</p>

<p>After <a href="http://stackoverflow.com/questions/39629064/issues-with-sending-lines-over-netcat-to-python/39629948#comment66565411_39629948">a comment</a> from <a href="http://stackoverflow.com/users/3246078/horse-hair">horse_hair</a>, I put put the server in a thread:</p>

<pre><code>import socket
import threading
import time

SOCK_BUFSZ = 4096

def udp_server(quit_flag):
    udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_sock.bind(('127.0.0.1', 50000))
    counter = 1
    while not quit_flag.is_set():
        print "counter = %s" % counter
        counter += 1
        data, addr = udp_sock.recvfrom(SOCK_BUFSZ)
        print data

def main():
    quit_flag = threading.Event()
    udp_thread = threading.Thread(target=udp_server, args=(quit_flag,))
    udp_thread.daemon = True
    udp_thread.start()
    try:
        while True:
            time.sleep(1.0)
    except KeyboardInterrupt:
        print "Exiting due to keyboard interrupt"
    quit_flag.set()
    udp_thread.join()

if __name__ == '__main__':
    main()
</code></pre>

<p>Everything still works.</p>

python sockets