Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Sending Structured Data | Working with Sockets
Practice
Projects
Quizzes & Challenges
Quizer
Challenges
/
Python Networking Basics

bookSending Structured Data

When computers exchange information, plain text is often not enough, so we use structured data that both sides can easily understand. The most common format is JSON, which is readable, lightweight, and maps naturally to Python dictionaries and lists.

Common structured data formats:

  • JSON, widely used in web APIs and modern applications;
  • XML, more verbose but still used in some systems;
  • CSV, simple format mainly for tabular data.
123456789101112131415161718192021222324252627282930313233343536373839
import socket import json # Data to send (a Python dictionary) data = { "username": "alice", "score": 42, "active": True } # Serialize dictionary to JSON string, then encode to bytes json_data = json.dumps(data).encode("utf-8") # Set up a server socket (for demonstration) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(("localhost", 65432)) server.listen(1) # Set up a client socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(("localhost", 65432)) # Accept connection on the server side conn, addr = server.accept() # Client sends JSON data client.sendall(json_data) # Server receives data received = conn.recv(1024) # Decode bytes to string, then deserialize JSON to dictionary received_data = json.loads(received.decode("utf-8")) print("Server received:", received_data) # Clean up sockets client.close() conn.close() server.close()
copy

The process you just saw, serialization and deserialization, is fundamental to sending structured data over a network. Serialization means converting a Python object, such as a dictionary, into a format that can be transmitted as bytes over a socket, typically a JSON string encoded into bytes.

On the receiving side, deserialization reverses this process by decoding the bytes back into a string and converting it into a Python object using json.loads. This allows complex data to be exchanged reliably between different programs, machines, or even different programming languages, making your network communication more flexible and extensible.

question mark

What is the main purpose of serialization when sending structured data over sockets in Python?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookSending Structured Data

Sveip for å vise menyen

When computers exchange information, plain text is often not enough, so we use structured data that both sides can easily understand. The most common format is JSON, which is readable, lightweight, and maps naturally to Python dictionaries and lists.

Common structured data formats:

  • JSON, widely used in web APIs and modern applications;
  • XML, more verbose but still used in some systems;
  • CSV, simple format mainly for tabular data.
123456789101112131415161718192021222324252627282930313233343536373839
import socket import json # Data to send (a Python dictionary) data = { "username": "alice", "score": 42, "active": True } # Serialize dictionary to JSON string, then encode to bytes json_data = json.dumps(data).encode("utf-8") # Set up a server socket (for demonstration) server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(("localhost", 65432)) server.listen(1) # Set up a client socket client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(("localhost", 65432)) # Accept connection on the server side conn, addr = server.accept() # Client sends JSON data client.sendall(json_data) # Server receives data received = conn.recv(1024) # Decode bytes to string, then deserialize JSON to dictionary received_data = json.loads(received.decode("utf-8")) print("Server received:", received_data) # Clean up sockets client.close() conn.close() server.close()
copy

The process you just saw, serialization and deserialization, is fundamental to sending structured data over a network. Serialization means converting a Python object, such as a dictionary, into a format that can be transmitted as bytes over a socket, typically a JSON string encoded into bytes.

On the receiving side, deserialization reverses this process by decoding the bytes back into a string and converting it into a Python object using json.loads. This allows complex data to be exchanged reliably between different programs, machines, or even different programming languages, making your network communication more flexible and extensible.

question mark

What is the main purpose of serialization when sending structured data over sockets in Python?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
some-alt