Is it Possible to Send a File And or Folder Over Python's Socket Module?

Essentially I'm making an application, the client checks if it has the needed files, if not it downloads them from the server. I'm wondering if I would be able to do that with Python's socket module, if so, how can I do such things?


Short answer: Yes.

Long answer: There are probably easier ways to do it if you have control of both ends. Using sockets directly is a somewhat low-level approach, and to get things working robustly, you would need to design a protocol to specify what file to request, have headers that specify length, etc, etc.

However, transferring files over a network is a problem that's been solved many times over, so there are several python modules to do it for you in much less code using a wide range of already developed protocols.

For example, with HTTP as the backend, the requests module module lets you do things like this:

r = requests.get('http://myserver/somefile.txt')
f = open('local_file_name.txt', w)

There are also python modules that would allow setting up a very simple HTTP server in very little code.

See here for one example (this actually doesn't require writing any code):

A simple test combining the two. Assumes Linux, same basic principle applies on windows. Open two consoles:

Console 1:

$ cd ~
$ mkdir http_test_server
$ cd http_test_server
$ echo "Hello World" > test_file.txt
$ python -m SimpleHTTPServer 8080 

Console 2:

$ python
>>> import requests
>>> import requests
>>> r = requests.get('http://localhost:8080/test_file.txt')
>>> r.content
'Hello World\n'

After running commands in Console 2, you should see output like this in console 1: - - [22/Jan/2015 10:42:54] "GET /test_file.txt HTTP/1.1" 200 -

Need Your Help

WYSIWYG editor gem for Rails?

ruby-on-rails ruby gem

Is there a good ruby gem for a WYSIWYG editor that will easily work with a rails app?

Why is Peer-to-Peer programming a hard topic to obtain good research for?

networking language-agnostic p2p

After reading a bit more about how Gnutella and other P2P networks function, I wanted to start my own peer-to-peer system. I went in thinking that I would find plenty of tutorials and language-agn...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.