Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am trying to send a data frame through socket. But I got some errors i try to remove them but can't. can anoyone help me

What I have tried:

i have tried this code
Python
<pre>
import socket
import pandas as pd
import pickle
df = pd.read_csv(r"C:\Users\DELL\OneDrive\Desktop\mythesisdataset.csv" ,engine='python',
                 names=[ 'SBP', 'DBP', 'HEARTRATE', "Temperature" ])
normal_df = (df [ (df.SBP > 120) & (df.DBP > 90) & (df.HEARTRATE < 100) & (df [ 'Temperature' ] < 100) ])
print(normal_df)
normal_df_bytes = pickle.dumps(df)

s = socket.socket()
host = socket.gethostname()
port = 12345

s.bind((host, port))

print("host name:", host, " socket name:", socket)

print("Waiting for Fog-node to connect...")
s.listen()
while True:
    c, addr = s.accept()
    print('Got connection from', addr, '...')
    bytes = c.send(normal_df_bytes)
    c.close()  # Close the connection

Python
import socket
import sys
import pickle

HOST, PORT = "123.123.123.123", 12345
print(sys.argv[0:])
data = " ".join(sys.argv[1:])

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

try:
    # Connect to server and send data
    sock.connect((socket.gethostname(), 12345))
    sock.sendall(bytes(data + "\n", "utf-16"))

    # Receive data from the server and shut down
    received = str(sock.recv(1024), "utf-16")
    pickle.load(received)
finally:
    sock.close()

print("Sent:     {}".format(data))
print("Received: {}".format(received))
Posted
Updated 6-Mar-22 21:53pm
Comments
Richard MacCutchan 6-Mar-22 11:14am    
What errors, and where do they occur?
Ayesha Tassaduq 6-Mar-22 12:55pm    
i get error on client side on line . pickle.load(received)
TypeError: file must have 'read' and 'readline' attributes this is the error
Richard MacCutchan 7-Mar-22 3:53am    
See my Solution below.
Ayesha Tassaduq 7-Mar-22 4:50am    
can you tell what i have to do ? or how i can send a data frame with socket if you can help ?
Richard MacCutchan 7-Mar-22 4:53am    
First you need to fix any errors in your code. Until you have a clean build of the code you cannot begin to test it.

1 solution

Look at the documentation: pickle — Python object serialization — Python 3.10.2 documentation[^]. The load method requires a file object as the first parameter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900