Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using the socket Module | Working with Sockets
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 alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookUsing the socket Module

Stryg for at vise menuen

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 alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1
some-alt