Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Using the socket Module | Working with Sockets
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
Python Networking Basics

bookUsing the socket Module

The socket module is the foundation for network programming in Python. It provides the tools you need to create, configure, and use network connections, allowing your code to communicate with other computers over the internet or a local network. With the socket module, you can build both clients and servers, send and receive data, and control how your application interacts with the network. Whether you want to fetch a web page, transfer files, or build a chat application, understanding how to use the socket module is an essential first step.

123456789101112
import socket # Create a socket object using IPv4 and TCP s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to example.com on port 80 (HTTP) s.connect(("example.com", 80)) print("Connected to example.com on port 80") # Always close the socket when done s.close()
copy

You start by importing the socket module and creating a socket with socket.socket(), choosing IPv4 (AF_INET) and TCP (SOCK_STREAM). You then connect to example.com on port 80, the standard port for HTTP traffic. After confirming the connection, you close the socket to release system resources. This follows the basic pattern, create, connect, communicate, close.

Note
Note

TCP is used here because it is reliable and ensures data is delivered correctly between the client and server.

question mark

Why is TCP used in the socket example instead of another protocol?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookUsing the socket Module

Свайпніть щоб показати меню

The socket module is the foundation for network programming in Python. It provides the tools you need to create, configure, and use network connections, allowing your code to communicate with other computers over the internet or a local network. With the socket module, you can build both clients and servers, send and receive data, and control how your application interacts with the network. Whether you want to fetch a web page, transfer files, or build a chat application, understanding how to use the socket module is an essential first step.

123456789101112
import socket # Create a socket object using IPv4 and TCP s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to example.com on port 80 (HTTP) s.connect(("example.com", 80)) print("Connected to example.com on port 80") # Always close the socket when done s.close()
copy

You start by importing the socket module and creating a socket with socket.socket(), choosing IPv4 (AF_INET) and TCP (SOCK_STREAM). You then connect to example.com on port 80, the standard port for HTTP traffic. After confirming the connection, you close the socket to release system resources. This follows the basic pattern, create, connect, communicate, close.

Note
Note

TCP is used here because it is reliable and ensures data is delivered correctly between the client and server.

question mark

Why is TCP used in the socket example instead of another protocol?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
some-alt