linux-nc-and-python-sockets

JSON

rev 5  |  foxhop  |  1347744223000  |  JSON

rev 4
rev 5
55 >>> # when you are finished run s.close()55 >>> # when you are finished run s.close()
5656
t57 t57The 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)
5858
5959
rev 4  |  foxhop  |  1347743435000  |  JSON

rev 3
rev 4
53 >>> # To learn more about sockets do:53 >>> # To learn more about sockets do:
54 >>> help( s )54 >>> help( s )
tt55 >>> # when you are finished run s.close()
5556
56 57 
rev 3  |  foxhop  |  1347742593000  |  JSON

rev 2
rev 3
26 26 
27Like the man page suggests all text submitted on either terminal A or B will app27Like the man page suggests all text submitted on either terminal A or B will app
>ear on both terminals.>ear on both terminals.
t28Conventionally the server "listening" for connections is typically labeled the st28Conventionally 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...
2929
3030
rev 2  |  foxhop  |  1347742562000  |  JSON

rev 1
rev 2
11Start listening (nc -l) for a connection on a port (31337).11Start listening (nc -l) for a connection on a port (31337).
1212
n13On terminal An13**On terminal A**
1414
15.. code-block:: bash15.. code-block:: bash
19Connect to (127.0.0.1) on a port (31337).19Connect to (127.0.0.1) on a port (31337).
2020
t21On terminal Bt21**On terminal B**
2222
23.. code-block:: bash23.. code-block:: bash
rev 1  |  foxhop  |  1347742503000  |  JSON

empty
rev 1
tt1linux-nc-and-python-sockets
2==============================
3 
4nc or netcat lets you create socket servers or connect to services via sockets.
5 
6The nc man page is excellent.  This first thing I tried was creating a simple-ch
 >at like program between two terminals.
7 
8Simple nc chat application
9=================================
10 
11Start listening (nc -l) for a connection on a port (31337).
12 
13On terminal A
14 
15.. code-block:: bash
16 
17 nc -l 31337
18 
19Connect to (127.0.0.1) on a port (31337).
20 
21On terminal B
22 
23.. code-block:: bash
24 
25 nc 127.0.0.1 31337
26 
27Like the man page suggests all text submitted on either terminal A or B will app
 >ear on both terminals.
28Conventionally the server "listening" for connections is typically labeled the s
 >erver.
29 
30 
31Use python to interact with nc
32=================================== 
33 
34On terminal A
35 
36.. code-block:: bash
37 
38 nc -l 31337
39 
40On terminal b
41 
42.. code-block:: python
43 
44 python
45 
46 >>> import socket
47 >>> s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
48 >>> s.connect( ( 'localhost', 31337 ) )
49 >>> s.send( 'python says hello nc' )
50 20
51 >>> s.recv( 30 )
52 'nc says hello python\n'
53 >>> # To learn more about sockets do:
54 >>> help( s )
55 
56 
57 
58