{"revision": {"id": "f3ba7700-2f95-11f1-b3d3-e86a64d24d78", "node_id": "f3b9a5c5-2f95-11f1-beb3-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "linux-nc-and-python-sockets\r\n==============================\r\n\r\nnc or netcat lets you create socket servers or connect to services via sockets.\r\n\r\nThe nc man page is excellent.  This first thing I tried was creating a simple-chat like program between two terminals.\r\n\r\nSimple nc chat application\r\n=================================\r\n\r\nStart listening (nc -l) for a connection on a port (31337).\r\n\r\n**On terminal A**\r\n\r\n.. code-block:: bash\r\n\r\n nc -l 31337\r\n \r\nConnect to (127.0.0.1) on a port (31337).\r\n\r\n**On terminal B**\r\n\r\n.. code-block:: bash\r\n\r\n nc 127.0.0.1 31337\r\n \r\nLike the man page suggests all text submitted on either terminal A or B will appear on both terminals.\r\nConventionally the server \"listening\" for connections is typically labeled the server.\r\n\r\n\r\nUse python to interact with nc\r\n=================================== \r\n\r\nOn terminal A\r\n\r\n.. code-block:: bash\r\n\r\n nc -l 31337\r\n\r\nOn terminal b\r\n\r\n.. code-block:: python\r\n\r\n python\r\n\r\n >>> import socket\r\n >>> s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )\r\n >>> s.connect( ( 'localhost', 31337 ) )\r\n >>> s.send( 'python says hello nc' )\r\n 20\r\n >>> s.recv( 30 )\r\n 'nc says hello python\\n'\r\n >>> # To learn more about sockets do:\r\n >>> help( s )\r\n\r\n \r\n\r\n\r\n", "source_format": "rst", "revision_number": 2, "created": 1347742562000}}