In this post, we’ll check out file transfer in Python 3 with file size verification check.
In many Python scripts you’ll come across the need for sending and receiving files over the network, so I decided to make this tutorial for a simple yet stable set of scripts to implement whenever needed.
We’ll only use standard Python libraries included with a default installation: socket, time, os and sys.
One nice little detail we’ll implement is file size verification, so that the receiving party can be sure they received the file with the exact amount of bytes the sender intended.
Welcome to part three of the Python control server series.
In part one we created our simple server and client with about 20 lines of code each (Python 3). Then in part two, we’ve added basic AES encryption to our traffic using pyAesCrypt.
Given the nature of encryption using pyAesCrypt, we we’re unable to receive any data on the server over 1024 bytes with encryption. The main reason for this is that length needs to be passed for the decryption method.
In this part we will be fixing this issue, so let’s get with it.
What exactly is the issue we have here? Why is less than 1024 bytes ok?
Both in our server and client we are sending 1024 bytes at a time. This means that whenever we send a message containing less than 1024 bytes, we won’t run into any issues.
Now considering we have to pass a length argument for decryption, if we send a message that is 1500 bytes, the server will receive the first 1024 bytes and attempt to decrypt it – resulting in an error.
This is where we have a few different options: receive all the encrypted data and then decrypt it all at once; or, receive smaller chunks of data and decrypt on-demand. Both options have their merits, I decided this latter one would be easier to program for a encryption noob like me. 🙂
It’s also worth mentioning that some of the libraries I used back then have not been properly ported over to Python 3. Overall it just makes a lot of sense to redo the project using more current libraries.
Why make a Python Control Server?
For many (ahem, very educational) reasons of course! Like for example, we are two days away from Christmas and many people enjoy taking vacations around this time. Well, why not setup a nice control server to have access to your systems while your away?
Sounds like a good idea time to me!
About Christmas though, I had my first batch of family visiting and let me tell you… its rather easy to get a cold when there’s a lot of people in the same household for hours at a time. So pardon my voice in the video.
Continuing from my last post, its time to implement the server thread.
A lot of pieces are already in place to handle the server threads, so we should be able to get it done without too much hassle.
There are some minor code changes in the server script which I will only be covering in the video above, so if you’re only following the blog make sure to download the code at the end of the post and compare the changes.
Let’s write some codes!
Server Handler Code
# Server handler
# This function will listen for connections as daemons
# and keep connected in background.
def ServerHandler(serverIP, serverPort, clientIP):
sh = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sh.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sh.listen(1) # Listen for 1 client, each server = 1 client
z,b = sh.accept()
print "\n[" + GetTime() + "] Connection from " + str(b)
msg = Receive(z)
if len(str(msg)): print "\n[" + GetTime() + "] " + msg
There isn’t much to the server handler. Basically we fire up a socket using the supplied arguments serverIP and serverPort – this port is the one supplied by the AllocAddress() function we created in the previous part (will basically add +1 to the same port of the main server).