Sending 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.
123456789101112131415161718192021222324252627282930313233343536373839import 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()
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.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 9.09
Sending 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.
123456789101112131415161718192021222324252627282930313233343536373839import 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()
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.
Takk for tilbakemeldingene dine!