Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Sending Structured Data | Working with Sockets
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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookSending Structured Data

Desliza para mostrar el menú

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
some-alt