Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pickle problem
#10
DPaul Wrote:you end up with 2... objects in the same pickle file.
Here is an example of this feature with sockets: A client connects to a server and sends three Python objects by successive calls to pickle.dump() in the same file-like object. The server receives the Python objects and prints them.

Server code:
# pickserver.py
import pickle
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)
        connection.shutdown(socket.SHUT_WR)
        file = connection.makefile(mode='rb')
        unpic = pickle.Unpickler(file)
        # Receive Python objects and print them
        while True:
            try:
                obj = unpic.load()
            except EOFError:
                break
            else:
                print(f'received: {repr(obj)}')
    finally:
        # Clean up the connection
        connection.close()
Client code:
# pickclient.py
import pickle
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print('connecting to {} port {}'.format(*server_address))
sock.connect(server_address)
sock.shutdown(socket.SHUT_RD)
file = sock.makefile(mode='wb')
pick = pickle.Pickler(file)

try:
    # Send Python objects
    data = [
        10,
        [2, 'spam', 'eggs'],
        (5, 7, 11),
    ]
    for obj in data:
        print(f'sending: {repr(obj)}')
        pick.dump(obj)

finally:
    print('closing socket')
    sock.close()
Output on the server side:
Output:
λ python paillasse/pf/pickserver.py starting up on localhost port 10000 waiting for a connection connection from ('127.0.0.1', 34014) received: 10 received: [2, 'spam', 'eggs'] received: (5, 7, 11) waiting for a connection
Output on the client side
Output:
λ python paillasse/pf/pickclient.py connecting to localhost port 10000 sending: 10 sending: [2, 'spam', 'eggs'] sending: (5, 7, 11) closing socket
Reply


Messages In This Thread
pickle problem - by DPaul - Sep-26-2022, 09:22 AM
RE: pickle problem - by buran - Sep-26-2022, 10:24 AM
RE: pickle problem - by DPaul - Sep-26-2022, 02:44 PM
RE: pickle problem - by buran - Sep-26-2022, 03:15 PM
RE: pickle problem - by DPaul - Sep-26-2022, 05:17 PM
RE: pickle problem - by deanhystad - Sep-26-2022, 09:47 PM
RE: pickle problem - by DPaul - Sep-27-2022, 06:10 AM
RE: pickle problem - by Gribouillis - Sep-27-2022, 07:41 AM
RE: pickle problem - by DPaul - Sep-27-2022, 08:49 AM
RE: pickle problem - by Gribouillis - Sep-27-2022, 09:35 AM
RE: pickle problem - by DPaul - Sep-27-2022, 02:38 PM
RE: pickle problem - by Gribouillis - Sep-27-2022, 03:43 PM
RE: pickle problem - by deanhystad - Sep-27-2022, 04:18 PM
RE: pickle problem - by DPaul - Sep-27-2022, 05:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 594 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  TypeError: cannot pickle n00sferatu 1 2,736 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 16,435 Aug-30-2021, 02:49 PM
Last Post: law
  Save/Loading using pickle Scordomaniac 4 3,149 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  computing entropy using pickle files baran01 2 2,509 Dec-30-2019, 09:45 PM
Last Post: micseydel
  Tkinter don't get ver from file via pickle storzo 2 2,639 Jul-31-2019, 03:50 PM
Last Post: storzo
  pickle docs say bytes in one place, strings in another Skaperen 2 2,221 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  pickle error SheeppOSU 4 11,127 Apr-20-2019, 04:50 PM
Last Post: SheeppOSU
  Using pickle.dump Friend 1 3,023 Feb-15-2019, 04:39 PM
Last Post: metulburr
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 2,452 Dec-19-2018, 05:41 PM
Last Post: CodeWolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020