===========================
linux-nc-and-python-sockets
===========================

.. raw:: html

   <table class="provenance-header" style="border: 0; border-collapse: collapse; margin: 0 0 16px 0; width: 100%;">

.. raw:: html

   <tr style="border: 0;">

.. raw:: html

   <td style="border: 0; vertical-align: top; padding: 0 24px 0 0;">

..

   | **Source:**
     https://foxhop.net/f3b9a5c5-2f95-11f1-beb3-e86a64d24d78/linux-nc-and-python-sockets
   | **Snapshot:** 2026-06-15T11:13:27Z
   | **Generator:** Remarkbox ``50b9d1e``

   *This is a thread snapshot. The living document lives at the source
   URI above — it may have been edited, extended, or replied-to since.*

.. raw:: html

   </td>

.. raw:: html

   <td style="border: 0; vertical-align: top; width: 200px; text-align: right;">

.. figure:: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAACkAQAAAAAxzrjsAAABdElEQVR42tWXsW0DUQxDhSyg/bfUBgof9YsUbgKwSL5h+O4KQkdRFF374Uz94adV1bM1up7Z9u1XfTq/eVrbuy3A6S6Btm8juMJqobYq3/FtDJffUbnTYVwRvDNiu3O4IpTGqdyeR3cCFwH8ODE9cEpEqG/6ttWX4bcAE7vCRxSdwTXgQIjHglHJ1KtuiQd3TNiILaSHRmDMMSJjNDJ6MAlunfj1OIf4pUbzSvHDJ6NfETB0DzVQe6jeumbBguQAFZm+HQmm17Wn+F1LAIdAdJvid3n3Qmp1Fhyqt7qeyJhnLlK4gFGwyeiUP5SXm+bBvn58Z+plEVMu2kASKV/H0Japu/UWw7U7TtklWXCxOb59QXx4bpzxyfOzhYAyI6E9hAQwCLfO8gj5ej9X20tSwX08JrdQRKX0S2BAws0I+wTzzu1kp78Uv859yNjhN8bv5VTbr5ddbF/47cehx2EnmNfX3nCbvpJ58sWzcZTK6ff//M/6BifS2XxUIbkmAAAAAElFTkSuQmCC
   :alt: Scan for living source

   Scan for living source

.. raw:: html

   </td>

.. raw:: html

   </tr>

.. raw:: html

   </table>

linux-nc-and-python-sockets
===========================

nc or netcat lets you create socket servers or connect to services via
sockets.

The nc man page is excellent. This first thing I tried was creating a
simple-chat like program between two terminals.

Simple nc chat application
==========================

Start listening (nc -l) for a connection on a port (31337).

**On terminal A**

.. container::
   :name: cb1

   ::

      nc -l 31337

Connect to (127.0.0.1) on a port (31337).

**On terminal B**

.. container::
   :name: cb2

   ::

      nc 127.0.0.1 31337

Like the man page suggests all text submitted on either terminal A or B
will appear on both terminals. Conventionally the server "listening" for
connections is typically labeled the server. In this case neither
terminal is a server because if either closes, both close...

Use python to interact with nc
==============================

On terminal A

.. container::
   :name: cb3

   ::

      nc -l 31337

On terminal b

.. container::
   :name: cb4

   ::

      python

      >>> import socket
      >>> s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
      >>> s.connect( ( 'localhost', 31337 ) )
      >>> s.send( 'python says hello nc' )
      20
      >>> s.recv( 30 )
      'nc says hello python\n'
      >>> # To learn more about sockets do:
      >>> help( s )
      >>> # when you are finished run s.close()

The constant socket.AF_INET creates a socket which allows us to connect
to an ip/name and port. The constant socket.AF_UNIX creates a socket
which allows us to connect via files? socket.SOCK_STREAM is the type of
socket (tcp/ip). socket.SOCK_DGRAM (data-gram or UDP)

--------------

| **Source:**
  https://foxhop.net/f3b9a5c5-2f95-11f1-beb3-e86a64d24d78/linux-nc-and-python-sockets
| **Snapshot:** 2026-06-15T11:13:27Z
| **Generator:** Remarkbox ``50b9d1e``
