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.
We’re off to part five of our little Python control server.
I had been postponing this, but its time we implement multiple clients!
I knew we had to do this right and that it would most likely bloat the scripts as well, so it would also require breaking it down into specific modules.
So yeah, that’s what we’re doing, lets get into it!
But before that, let me give you guys some context into the code…
Modular Programming
Many times when working on a project, as it begins to expand and get into a few hundred lines of code we need to start breaking it up into modules.
For our project, so far, it seems most of the functionality is divided between server configuration and data encryption.
I figure, just for the sake of development and teaching proper methods to keep working in a program without feeling like its a labyrinth, we’ll go ahead and separate our code into a few modules.
[ Part 1 – Part 2 – Part 3 – Part 4 – Part 5 – Part 6 – Part 7 – Part 8 ]
What’s going on guys?
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.
Traffic Encryption
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. 🙂
We’re off to another series: Python Control Server.
Yes, there’s three series going on at once in the blog, so what! 😀
I like having multiple things going so that I don’t have to think much whenever I have some free time to code. I can just sit down and choose whichever series/project to work on at the moment.
Anyway, many people emailed me to post my old videos (from like 10 years ago) from this same series – written in Python 2.X.
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.
Control Server
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.bind((serverIP, serverPort))
sh.listen(1) # Listen for 1 client, each server = 1 client
z,b = sh.accept()
if (z):
print "\n[" + GetTime() + "] Connection from " + str(b)
while True:
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).
Recent Comments