Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Networking
  5. Socket Module

Socket Module

Sockets are interior endpoint built for sending and receiving data. It is a combination of an IP address and a port.

A single network has two sockets one for each communicating device or a program.

Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC).

A single device can have ‘n’ number of sockets based on the port number that is being used.

Lets us see some common port numbers and a protocol.

  • HTTP – 80
  • FTP – 20
  • NNTP – 119
  • SMTP – 25
  • Telnet – 23

This is all about socket.

In python, we have a module named as socket which helps in networking between client and the server.

We can easily import socket module in python. We don’t need to install it from anywhere. If you are downloading it through pip install it means you are installing os from pip install.

syntax:-

import socket

socket module has a function known as socket(). It is used to create a socket.

syntax:-

a = socket.socket(socket_family, socket_type, protocol)

socket_family- This is either AF_UNIX or AF_INET.
Socket_type- This is either SOCK_STREAM or SOCK_DGRAM.
protocol- This is usually left out, defaulting to 0.

This is all about socket module in python.

Was this article helpful to you? Yes No

How can we help?