Starting network programming with Python

The Python socket module provides an interface to the Berkeley sockets API (another name for internet sockets). Programming networks in Python depends on the socket objects. To create an object of this type in Python, we must use the socket.socket() function that's available in the socket module, with the socket_0 = socket.socket(socket_family, socket_type, protocol=0) syntax.

Let's see a detailed description of the parameters:

  • socket_family: This is the family of protocols that is used as a transport mechanism. These values are constants, such as AF_INET, PF_INET, PF_UNIX, and PF_X25.
  • socket_type: The type of communication between the two ends of the connection. SOCK_STREAM is usually used for connection-oriented protocols and SOCK_DGRAM for protocols without connections.
  • protocol: Normally 0, this parameter is used to identify the variant of a protocol within a family and socket type.

These are the methods of socket objects:

  • socket.bind(): This method binds an address (hostname, port number) to a socket
  • socket.listen(): This method configures and starts a TCP listener
  • socket.accept(): This function passively accepts a TCP client connection, waiting until the connection arrives

For more detailed information regarding the methods in the socket module, you can check out the documentation at https://docs.python.org/3/library/socket.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset