

Then, in an infinite cycle, it reads data from the socket in batches of 1024 bytes using method recv() until it returns an empty string. When this happens, it creates a new socket and returns it together with the client's address. To accept an incoming connection we call accept() method which will block until a new client connects. Here we create a server socket, bind it to a localhost and 50000 port, and start listening for incoming connections. Here is an example of Echo server from documentation: import socket send() and recv() are common for both types. connect() is specific for client sockets. It returns a socket object which has the following main methods:īind(), listen() and accept() are specific for server sockets. S = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Here’s a Python socket example: import socket To create a TCP-socket, you should use socket.AF_INET or socket.AF_INET6 for family and socket.SOCK_STREAM for type. It accepts family, type, and proto arguments (see documentation for details). To create a socket, there is a function called socket. This interface is common across different programming languages since it uses OS-level system calls. The Python Standard Library has a module called socket which provides a low-level internet networking interface. A network socket is an endpoint of an interprocess communication across a computer network.
