I'm starting to use sockets to send a file. I learned I can send in 1024 "chunks" through the socket stream, and piece it back on the server side. I set the server to first get a string containing the size of the file it will receive, and as it reads to compare to see if the read is finished or not.
Client code is:
BUFFER_SIZE = 1024
limit = os.path.getsize("test.db") #4096 bytes
currentamt = BUFFER_SIZE
f = open("test.db", "rb")
l = f.read(BUFFER_SIZE)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(str(limit)) #first, send the limit
while(1):
s.send(l)
if currentamt >= limit:
break
l = f.read(BUFFER_SIZE)
currentamt += BUFFER_SIZE
print "still sending.."
s.close()
print "Done."
Server code is as such:
while True:
conn, addr = s.accept()
print "Connection address: ", addr
f = open('rec.db', 'wb')
while 1:
limit = conn.recv(20) #this will get your limit size
limit = limit.rstrip()
limit = float(limit) #error in question
print limit
l = conn.recv(BUFFER_SIZE) #get byte bufferred back
while (1):
if currentamt >= limit:
f.write(l)
break
f.write(l)
l = conn.recv(BUFFER_SIZE)
currentamt += BUFFER_SIZE
f.close() #close your file after you're done
conn.close()
s.close()
If I change limit in server from 20 to 10, I am able to convert to a float, but then I get an error after the print statement! Also, the print limit does work before the float(limit) statement, which confuses me.