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) f.write(r.content) f.close()
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): http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python
A simple test combining the two. Assumes Linux, same basic principle applies on windows. Open two consoles:
$ cd ~ $ mkdir http_test_server $ cd http_test_server $ echo "Hello World" > test_file.txt $ python -m SimpleHTTPServer 8080
$ 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:
127.0.0.1 - - [22/Jan/2015 10:42:54] "GET /test_file.txt HTTP/1.1" 200 -