Click here to Skip to main content
15,357,204 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.
Ayesha Tassaduq 7-Mar-22 9:05am
   
problem is the error.. i try many times but did not remove the error. Can you share your mail address ?
Richard MacCutchan 7-Mar-22 9:11am
   
Show us the error and the line(s) of code that causes it. We cannot guess what is going on in your code.
Ayesha Tassaduq 7-Mar-22 9:19am
   
pickle.load(received) I receive error on this line
Richard MacCutchan 7-Mar-22 9:21am
   
And I explained why that is in the Solution below. Please go and read the documentation to see how you can load from a string.
Ayesha Tassaduq 8-Mar-22 8:04am
   
problem solved Thank you.

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.
   

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