Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Using the socket Module | Working with Sockets
Practice
Projects
Quizzes & Challenges
Frågesporter
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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookUsing the socket Module

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1
some-alt