O'Reilly logo

Learning Python Network Programming by Sam Washington, Dr. M. O. Faruque Sarker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required