SOLVED: Improper loop and difficulty sending files - Sockets

SOLVED: Improper loop and difficulty sending files - Sockets

Postby TheFunk » Mon Nov 25, 2013 5:01 pm

I'm trying to wrap my head around the idea of stream protocols and sockets. I've been following along with tutorials, so I set up a sample program (important parts below) to test sending files over raw sockets. I set my buffer size to 1024.

My client's loop looks like this:
Code: Select all
while True:
    data = file.read(1024)
    if not data:
        break  # EOF
    else:
        sawkit.sendall(data)
sawkit.close()
file.close()


Whereas my server's loop looks like this:

Code: Select all
while True:
   conn, client = sawkit.accept()
   data = conn.recv(1024)
   testfile.write(data)
   if not data:
        break
   conn.close()
   testfile.close()


At the moment, this code only writes 1KB of data, cutting out large chunks of files. I'm assuming this has something to do with my loop structures, but I can't figure out why the program doesn't loop properly.

In addition, I'm opening my files read(r) and write(w) only, respectively, along with the b option for immediate use with buffers. I'm finding however that any file with any sort of extension refuses to work with this method of file transfer at all. Only plain text files seem to be able to be sent, which is odd. Is this a matter of encoding or something else entirely?

Thanks for the time, per usual!

EDIT: The client appears to be looping properly. It's the server that refuses to loop. Hurray debugging!

EDIT 2: Wooooowwwwww. It's a Monday. I needed a second while loop server side, because otherwise the socket was closing immediately following the first read. Problem solved. Sorry about that folks...
TheFunk
 
Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: SOLVED: Improper loop and difficulty sending files - Soc

Postby Kebap » Wed Nov 27, 2013 11:45 am

TheFunk wrote:Problem solved. Sorry about that folks...


Thanks for posting the update! Good to know the reason. :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 387
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to Networking

Who is online

Users browsing this forum: No registered users and 2 guests