Jump to content
  • Advertisement
Sign in to follow this  
PhlashStudios

Writing Files to a Remote Client in Python

This topic is 4477 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 it

import socket

ISSocket = 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 saves

import socket

ISSocket = 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!