Python socket programming

TCP Sockets

Create a TCP socket:

import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    ...

AF_INET specifies an Internet IPv4 type socket, while SOCK_STREAM specifies a TCP stream type socket.

Client and Server sockets life cycle

Server creates a listening socket, by the way of the following method calls:

  • socket()
  • bind()
  • listen()
  • accept()

Client creates a socket with the socket() method as well, the calls the connect() method to establish a connection with the server. Once the connection is established, data are exchanged with the send() and recv() methods. Connection is closed with the close() method.


© Alessandro Dotti Contra :: VAT # IT03617481209 :: This site uses no cookies, read our privacy policy for more information.