|
|
| >>> # when you are finished run s.close() | | >>> # when you are finished run s.close() |
| | | |
| t | | t | 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 t |
| | | o connect via files? socket.SOCK_STREAM is the type of socket (tcp/ip). socket |
| | | .SOCK_DGRAM (data-gram) |
| | | |
| | | |
|
|
| >>> # To learn more about sockets do: | | >>> # To learn more about sockets do: |
| >>> help( s ) | | >>> help( s ) |
| t | | t | >>> # when you are finished run s.close() |
| | | |
| | | |
|
|
| | | |
| Like the man page suggests all text submitted on either terminal A or B will app | | Like the man page suggests all text submitted on either terminal A or B will app |
| ear on both terminals. | | ear on both terminals. |
| t | Conventionally the server "listening" for connections is typically labeled the s | t | Conventionally the server "listening" for connections is typically labeled the s |
| erver. | | erver. In this case neither terminal is a server because if either closes, both |
| | | close... |
| | | |
| | | |
|
|
| Start listening (nc -l) for a connection on a port (31337). | | Start listening (nc -l) for a connection on a port (31337). |
| | | |
| n | On terminal A | n | **On terminal A** |
| | | |
| .. code-block:: bash | | .. code-block:: bash |
| Connect to (127.0.0.1) on a port (31337). | | Connect to (127.0.0.1) on a port (31337). |
| | | |
| t | On terminal B | t | **On terminal B** |
| | | |
| .. code-block:: bash | | .. code-block:: bash |
|
|
| t | | t | 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-ch |
| | | at like program between two terminals. |
| | | |
| | | Simple nc chat application |
| | | ================================= |
| | | |
| | | Start listening (nc -l) for a connection on a port (31337). |
| | | |
| | | On terminal A |
| | | |
| | | .. code-block:: bash |
| | | |
| | | nc -l 31337 |
| | | |
| | | Connect to (127.0.0.1) on a port (31337). |
| | | |
| | | On terminal B |
| | | |
| | | .. code-block:: bash |
| | | |
| | | nc 127.0.0.1 31337 |
| | | |
| | | Like the man page suggests all text submitted on either terminal A or B will app |
| | | ear on both terminals. |
| | | Conventionally the server "listening" for connections is typically labeled the s |
| | | erver. |
| | | |
| | | |
| | | Use python to interact with nc |
| | | =================================== |
| | | |
| | | On terminal A |
| | | |
| | | .. code-block:: bash |
| | | |
| | | nc -l 31337 |
| | | |
| | | On terminal b |
| | | |
| | | .. code-block:: python |
| | | |
| | | 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 ) |
| | | |
| | | |
| | | |
| | | |