Writing Files to a Remote Client in Python

Started by
17 comments, last by Kylotan 18 years, 1 month ago
Hey everyone, I am working with Python and am trying to create a simple file sharing app using a client/server model, to test out its network capabilities. I have everything working fine with the login/username/password but have come up against a brick wall in the actual file-share department. My idea was to use the open(~) command to open a file on the server and then the write(~) command to write that file to the client. The problem is actually writing the file to the client, can anyone help? I am not using any packages like SimpleXMLRPCServer, just sockets. Thanks in advance. [Edited by - PhlashStudios on March 16, 2006 11:39:29 AM]
<a href="http://eyeredux.com/campaigns/1/redirect">Hello!</a>
Advertisement
Straying from your technical question slightly, if you really want to see python's power when it comes to network programming, maybe you should give twisted-matrix a try. Your file-sharing app might be much easier to write this way.
Thanks, do you know where I can get any good tutorials...besides buying the book? Especially if they pertain to file sharing.
<a href="http://eyeredux.com/campaigns/1/redirect">Hello!</a>
Good question. I forget the exact path I took while learning to use twisted. I would begin by reading some of the core developer how-to guides. I don't recommend reading them all, or even from top to bottom, as a lot of that information probably won't be important to you. I think I started with Perspective Broker, which will probably be the kind of network programming you're looking for.
You've not given much detail with the problem. Which part do you not know how to do? On one side you open the file, and send it down an open socket, repeating calls to send() until it's all done, then close the socket. On the other side you create a file, read from the socket, repeating calls to recv() until you receive zero or an exception, then write that data to the file.

If you're really just experimenting to learn Python networking, start with something less ambitious and just send messages from one machine to the other. Once you have that working, file transfer is the next step.
The part that is not working for me, is when I try to send the file to the socket it tells me that I can't send whole files and I'm not sure how to break it up, could you provide some example code?...Like for example sending a text file from the server to the client?
<a href="http://eyeredux.com/campaigns/1/redirect">Hello!</a>
Dunno if this is what you want, but there's a sendall() method of socket objects that will keep trying to send until it's done or an error occurs.

You may also be getting the error because a file is not a string, which is what socket.send() expects. Use file.read() to retrieve the string contained in the file.
---New infokeeps brain running;must gas up!
What about if the file is for instance a picture or a music file?
<a href="http://eyeredux.com/campaigns/1/redirect">Hello!</a>
A file is a file. It's a long list of bytes on a disk, and your programming language doesn't care what it actually might mean to other applications. All it has to do is pick those bytes off the disk and send them down the socket.
I tried to write a simple client server interface to test out the idea and what kept happening is that I would receive a file called test.bmp, but the file was blank....
server:
#test file transfer server#server listens for connections from a client and when a client connects sends a #picture to itimport socketISSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)ISSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)ISSocket.bind(('', 1337))ISSocket.listen(1)while 1:    clientsock, clientaddr = ISSocket.accept()    print "Got connection from ", clientsock.getpeername()    clientsock.sendall('q')    print "this far"    file1 = open("C:\Documents and Settings\Dan Shipper\My Documents\My Pictures\mole.bmp", "w+")    for line in file1:        clientsock.sendall(line)    print "file sent"


client:
#test file transfer client#client connects to the server which transfers an image to client, client savesimport socketISSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)ISSocket.connect(('192.168.1.65', 1337))while 1:    try:        buf = ISSocket.recv(10000)    except socket.error, e:        print "error recieveing data"    if not len(buf):        print "not len buf"        break    file1 = open("test.bmp", "a+")    file1.write(buf)    print "file written"    print buf

Any ideas? thanks in advance
Edit: Also the files that I am trying to send turn up empty, they are still there, just with no bites left in them.
<a href="http://eyeredux.com/campaigns/1/redirect">Hello!</a>

This topic is closed to new replies.

Advertisement