Netcat Python and UDP


Netcat usually written as nc on most Linux systems, if often desctibed as the Swiss army knife of packet operations. I have been having a little trouble finding something... so this mini guide is to try and help me remember.


My data is being generated by a Python program, and I am sending the data as UDP - on a broardcast.

Why ?

I would like the possibility of multiple clients to connect to the stream - and each client may perform seperate functions on the data stream.


My development enrinoment is as follows

  • Mac Book Pro i5 and i7
  • OS 10.10.3
  • Python 3.3.4 - in Venv
  • nc - 0.7.1
  • ip address is
  • broardcast address of

Code Example

This is my Python Broardcast class - nothing too special here.

Note: This is Python3 format code - due to the bytes string formatting.

# Send UDP broadcast packets
import sys, time
from socket import *

class broardcast:

    def __init__(self):
        self.s = socket(AF_INET, SOCK_DGRAM)
        self.s.bind(('', 0))
        self.s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        self.MYPORT = 50000

    def send(self,data):
        print (".")
        self.s.sendto(bytes(data,'UTF-8'),("", self.MYPORT))

if __name__ == "__main__":
    for i in range(50):


Again - nothing difficult....


So where is the issue ?

I want to verify what this is producing..

Python Client

This is the Python Receiver for the Broardcast

import socket

MCAST_PORT = 50000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('', MCAST_PORT))
while True:
  print (" "+str(sock.recv(10240)))

Other receivers

But .. these are rather heavy - I want to view this with netcat.

I can see these packets

  • Python Client
  • wireshark
  • tshark
  • netcat

I have verified the data using the first 3 - but I want to be able to see the data with netcat.


Despite many warning about nc (netcat) not working on Mac - I have had no issues so far.

My data is being generated by

    for a in $(seq 10)
         echo "Loop $a running"
         sleep 5s
The netcat command to receive

* Broardcast


``` bash
   nc -l -s -p 50000 -u 

command line args

  • -s Source the broardcast address
  • -p Port 50000, a non priv-port
  • -l Listen

You should see output like


Which is the data without line sperators

Whats next ?

Apache Spark real time processing..... using yep the network data described above.