Working with TCP sockets

Creating a socket object in Python is very straightforward. You just need to import the socket module and call the socket() class:

from socket import*
import socket

#create a TCP socket (SOCK_STREAM)
s = socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0)
print('Socket created')

Traditionally, the class takes plenty of parameters. Some of them are listed in the following:

  • Socket family: This is the domain of socket, such as AF_INET (about 90 percent of the sockets of the Internet fall under this category) or AF_UNIX, which is sometimes used as well. In Python 3, you can create a Bluetooth socket using AF_BLUETOOTH.
  • Socket type: Depending on your need, you need to specify the type of socket. For example, TCP and UDP-based ...

Get Learning Python Network Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.