Skip to content

pysendfile

August 17, 2011

TL;DR Frustrated I couldn’t send a file to a friend over a bash terminal, I developed pysendfile, a Python based UNIX tool for file sharing.

Given my extremely heavy involvement in school (students who take Advanced Placement classes, and lots of them, know how I feel) and student government, and my small attempt to have a life, I often don’t have the time to sit down and attempt personal programming projects. However, the other night, inspiration struck in the best way–I had something I wanted to do, and I couldn’t. So I made it.

I was Skyping with my friend Sam Rose (see his blog here) and casually experimenting with a library called pygame. I finished a small, but interesting script, and wanted to send it to Sam. As an obstinate developer and Linux user, I did not want to leave my bash terminal to do this. To my behest, the sendfile tool was not intended to send files over the Internet–in fact, I couldn’t find anyway to do this. I was determined–I would not leave my terminal to send my file. So, in the same style of so many hackers before me (this kind of hacker, not a cracker, just to clarify) I decided that, rather than spend one minute navigating Skype’s GUI to send a file, I would spend five hours developing a Python script to send files over a bash terminal.

Well, first, I had to learn parsing command line arguments–I’m new to Python. With the help of the Python docs and an extremely helpful Dive into Python tutorial, and a couple hours work, I had working command line input in traditional UNIX format: –help’s, -v’s, and all. Next, it was networking. The Python docs (here and here) did much better for sockets, and, after some coding and testing on the localhost, I had cobbled together a working UNIX tool for sending files! One problem–it only sent across one packet of data. Max file size of ~1KB. As a Java programmer used to sockets that could stream data, this came as a bit of a surprise. However, seeing as it was 2am, and I had to be up at 5:30 for a state student government event the next day, it was time to head to bed.

Of course, as for any programmer, it wasn’t as simple as just putting it down. Spending the next morning’s car ride googling Python networking, and running across a few useful posts on StackOverflow, I prepared some quick psuedocode on how I would split the data across multiple packet (spoiler alert–I just used array slicing to cut it in groups of 512 characters and append them on the other end). A few days of student government, some coding, and a bit of debugging later, I had a fully working UNIX tool for direct P2P file sharing.

As an overview, here is the -h output:
Usage: sendfile [OPTION] ...
Sends a file to another sendfile user on the other end.

-a, --address The adress to send to
-f, --file Filename to send
-h, --help Print this usage and quit
-p, --port The port to send or wait on
-s, --send Flag telling sendfile to send
-w, --wait Flag telling sendfile to wait

And in practice, the bash commands would be (from the README):
On the receiving end-
$ pysendfile --wait --port 5000
On the sending end-
$ pysendfile --send --file example.txt --address 127.0.0.1 --port 5000

I may later post a small overview of how the networking functions. Either way, if you would like to see the source, or use pysendfile yourself, head on over to https://github.com/mrjordangoldstein/pysendfile, and download the latest version. In the future, I hope to implement saved addresses, always up, queue based waiting and sending, and an interactive shell.

Let my know what you think down in the comments–of the program or my writing!

Advertisements

From → Programming

2 Comments
  1. Why not use cat over nc? Or SCP?

    • I was unfamiliar with them or how they work. Looking them up, cat over nc seems like an easy enough solution, wish I’d known about nc! As for SCP, I’ve never used ssh at all, I didn’t really want to ssh into my friends machine, so that isn’t the best solution. Overall, I think my script is a bit more elegant of a solution than either of those, and I’m glad a wrote it, but I’m extremely glad there are ways to do it in a stock bash environment!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: