2

Greetings and apologies in advance if it looks a real novice question. I am new to python, or programming for that matter. I am writing a code that sends data from client to server. The data I need to send is from an csv file, which has around 10,000 rows. Currently I am sending the data in a large buffer as a whole, but I would prefer to send it row by row and also receive it the same way. I am not sure if I should use the split() function or there are any better ways to do the same thing.

The client...

import socket

HOST = 'server IP'        
PORT = 42050              
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
f = open('csvfile.csv', 'rb')
l = f.read(409600)
while True:
    print l    
    s.send(l)    
    break
f.close()
print "Done Sending"
s.close()

The server...

import socket

HOST = 'local IP'      
PORT = 42050              
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print "Server running", HOST, PORT
s.listen(5)
conn, addr = s.accept()
print'Connected by', addr
while True:
    data = conn.recv(409600)          
    print repr(data)
    if not data: break
print "Done Receiving"
conn.close()

Thanks in advance for the help.

1 Answer 1

1

im not sure what your question actually is ... but its pretty easy to send and receive lines

#client.py
for line in open("my.csv"):
    s.send(line)


#server.py
def read_line(sock):
    return "".join(iter(lambda:sock.recv(1),"\n"))

iter(lambda:sock.recv(1),"\n")

is essentially the same as

result = ""
while not result.endswith("\n"): result += sock.recv(1)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! Now that you explained it looks simple enough.
In def read_line(sock) where does "sock" come from? How does this method look incorporated into OPs server.py code exactly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.