S
S
Security Studies
Search…
Basic Sockets
A network socket is basically a endpoint of two or more processes communicating over a network.

Sockets in Python

To create a socket in python, there is a function called socked. It accept family, type and proto arguments.
To create a TCP-socket, you should use socket.AF_INET or socket.AF_INET6 for family and socket.SOCK_STREAM for type.
  • The family will specify the designated addresses that your socket can communicate with. In case of AF_INET is ipv4 and AF_INET6 is ipv6. You can also use families as AF_UNIX, AF_IPX, AF_IRDA and AF_BLUETOOTH, for example.
  • The type will specify... the type. SOCK_STREAM stands for TCP socket and SOCK_DGRAM for UDP socket. There are also another possibilities but these two will be enough for 99% of the times.
So, creating a socket is this simple
import socket
s = socket.socket(AF_INET, socket.SOCK_STREAM)
This returns an object (that we called s) that has the following main methods:
  • bind()
  • listen()
  • accept()
  • connect()
  • send()
  • recv()
bind(), listen() and accept() are specific for server sockets, while connect() is specific for client sockets and send() and recv() are common for both types.

Creating a TCP Socket Server

Here is an example of a server that echoes everything that it receives.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 9999))
s.listen(1)
conn, addr = s.accept()
while 1:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
conn.close()
As you can see by the code, we first create a server socket, like before. Then, we bind it to localhost and port 9999 and sets it to listen for incoming connections, that means that every application in your pc that tries to communicate with your localhost at port 9999 will "talk" with this socket. When this application connects itself to the socket, the server will generate a connection and the address of the client, or in this case, the application.
Then, while the connection exists, the server will receive (or recv) batches of 1024 bytes (note that this doesn't mean that the client will need to send 1024 bytes).
After that, it sends back all the data to the client using sendall, that in this case will use batches of send internally until all the data is sent.
Then, after the loop, it closes the connection.
Notice that in this example only one connection can be served at a time, since there is no accept() in the cycle.

Creating a TCP Socket Client

A client-side code looks much simpler
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 9999))
s.sendall('Hello world!')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
As before, a socket object is created and attributed to s.
Then, it connects to the server running at localhost and port 9999, sending the message Hello world! using sendall, already mentioned.
Following that, it receives a batch of 1024 bytes of data from the server and closes the connection.
In the end, it prints Received, together with the representation as a string of the data received.