Twisted Python RDT/RTSP Library and Stream Downloader

Twisted is a great framework for Python, but they don’t support RDT or RTSP protocol used for streaming Real video. The RDT/RTSP handler in MPlayer is a bit buggy and suffers from disconnect problems when receiving RTSP packets after switching to raw mode for RDT (though perhaps such occurrences are out of specification, they do still occur). Thus, I set off to create my own.

The library includes support for RTSP, RDT, SDPP, ASMRP, and RMFF, and it is all in pure Python and Pythonically coded where possible (see sdpp.py). It requires Python 2.6 and the Twisted Python library. The library has only one known bug, and I can’t for the life of me find it. Perhaps another set of eyes will effect a solution.

To use the library to simply download an RTSP stream:
python rdt.py -u <URL> -f <FILENAME>

If you like this post, please comment. Commenting is fast, anonymous, and greatly appreciated.
Download the Twisted Python RDT-RTSP Library here

Automatically Download Songs from Grooveshark

Because manually downloading songs from Grooveshark is too tedious, here’s a nice process to automatically save all the songs you listen to on Grooveshark. Unlike other methods, this does not use any extra bandwidth so you don’t need to listen to the song and download it, once is enough. » Read more…

Downloading MP3 Songs From Grooveshark

A friend of mine recently introduced me to Grooveshark, a website offering streaming radio similar to Pandora but which allows you to specify what song you would like to listen to rather than listening in a guided method as Pandora offers. I wondered if it was possible to save songs which are streamed from Grooveshark.


» Read more…

GIMP Resize and Match DPI in Scheme

Resize-match-dpi is a GIMP Script-Fu script written in Scheme to resize or scale an image along with scaling the DPI. Let’s consider an image being scanned which is 4.75″ x 4.70″. We scan the image at 300 DPI with a pixel resolution of 1425 x 1409 and we want to scale it down to 1280×1266. Using IrfanView, XNView, GIMP, and other programs will rescale the image, but the DPI will still be 300. This means when the image is viewed on a monitor or printed out it will appear too small (4.27″ x 4.22″). This script scales the DPI to match the new pixel resolution so it will still appear as 4.75″ x 4.70″.

If you like this post, please comment. Commenting is fast, anonymous, and greatly appreciated. » Read more…

TinyPic Command Line Uploader in AWK

This AWK script uploads an image to TinyPic.com and returns the direct url to the uploaded image.

Example script usage:

$ awk -f tinypic.awk The_Grass_Snake_-_Natrix_natrix.jpg
http://i39.tinypic.com/dlnb6r.jpg

Let me start by saying I have no idea why I wrote this in AWK. I’ve only used AWK once before for a very short one-liner. I find AWK to be better than Perl, but worse than Python. It does make for a light and portable language: the Windows AWK executable (available here) is only 345 KB, combine with Windows cURL (available here) which is 405 KB and you’re still under a meg for both. My bloated Python install is over 100 MB and I just installed Strawberry Perl which is 174 MB.

If you like this post, please comment. Commenting is fast, anonymous, and much appreciated. » Read more…

Xdsl: Using an Original Xbox as a Network Attached Storage Device (NAS), SSH server, BitTorrent client, and more

X-DSL is a Linux distribution for the original Xbox. The old Xbox sells for about $35 on eBay and can be used as an NAS for both IDE and SATA hard drives and can be used as a BitTorrent client.

For the sake of brevity, I will assume the Xbox already has XBMC installed and the ability to FTP files to the Xbox and execute xbe files.

Software used:
Xdsl iso, and use 7-zip to extract the iso
SSH/Putty
FileZilla or any FTP client
RealVNC

Part 1: Uploading Xdsl via FTP
Extract the files in the the Xdsl iso. Create an FTP session with the Xbox. Copy the KNOPPIX folder from the extracted iso to the Xbox’s E: drive. Open the KNOPPIX folder on the Xbox and copy minirt24.gz and linux24 to this folder. E:\KNOPPIX should now have three files, one of which is named KNOPPIX, and no further subdirectories. Also copy README-FTP\linuxboot.cfg to E:\linuxboot.cfg. Now make a folder in the E:\Apps directory called simply Linux and copy the default.xbe file to E:\Apps\Linux\default.xbe.

In total, four files are copied and the overall structure is:
E:\linuxboot.cfg
E:\Apps\Linux\default.xbe
E:\KNOPPIX\KNOPPIX
E:\KNOPPIX\linux24
E:\KNOPPIX\minirt24.gz

In XBMC run the Linux default.xbe. The Xromwell blue screen will appear. Select the hard drive and press A. It should boot directly into Xdsl.

Part 2: Installing Xdsl
The D-pad and left joystick move the mouse. Press Y to right-click the desktop and go to System > Daemons > ssh > start. Open putty/ssh and connect to the Xbox with username admin and password xbox. Then run the command xbox-loopbackinstall.

ssh root@192.168.1.101
root@192.168.1.101's password: xbox
xbox-loopbackinstall

Go through the menus and when it asks for the size of the loopback filesystem, change it from 200 to 600. Continue through selecting for the defaults. After it’s finished, reboot the Xbox:

reboot

Part 3: Configuring Xdsl for SSH and VNC
After the Xbox is done rebooting, go back in Xbmc and run Xromwell again and select the hard drive. This time a menu will appear. Choose XDSL Loopback, the second choice. Start the ssh daemon again (right-click the desktop and go to System > Daemons > ssh > start). Connect to the Xbox using Putty/ssh again. Users who are using ssh will need to remove the key from their system:

ssh-keygen -R 192.168.1.101

To set the ssh daemon to start when Xdsl boots, enter the following. If you are using Putty on Windows, copy it to the clipboard then right-click the Putty window to paste.

ln /etc/init.d/ssh /etc/rc5.d/S20ssh -sfn

Now on the Xbox go right-click the desktop and go to MyDSL > MyDSL Browser. In the left panel expand + net and scroll down and select vnc4x-1.4.6.dsl. Click the Install Selected button. Close out of the MyDSL Browser and right-click the desktop and go to MyDsl > vncserver > vnc4x0. Start up the RealVNC client and enter the Xbox’s IP and click connect.

Part 4: Installing BitTorrent
Now, using RealVNC, right-click the desktop XDSL Upgrades > Upgrade to GNU Utils, then XDSL Upgrades > Enable Apt. Right-click desktop, MyDSL > MyDSL Browser. Install net > bittorrent-cli.dsl.

Now in the ssh session enter:

ln -sfn /usr/bin/python2.2 /usr/bin/python

Mount the E: drive, then open a screen session and su to the regular user (dsl):

mount /dev/hda50
mkdir /mnt/hda50/torrents
apt-get install screen
screen
su dsl

BitTorrent can be run using the following command.

$ btdownloadcurses --saveas /mnt/hda50/torrents --minport 52130 --maxport 52140 CompletelyLegalLinuxDistribution.torrent

Note: VNC uses about 70% of the CPU. Only run it when you have to; use SSH.

To start more than one torrent, press [Ctrl]+[A], then [C] and type the command with the next torrent. [Ctrl]+[A],[P] and [Ctrl]+[A],[N] go back and forth between the two buffers.

That’s all for now. You can use SFTP to access the hard drive, SSH, VNC, and BitTorrent. Let me know in the comments if you enjoyed this tutorial. Comments are always appreciated.

Part 5: Serving files using Samba
Per a request in the comments, here’s how to have the Xbox serve files using Samba.

In Xdsl, right-click the desktop and go to MyDSL > MyDSL Browser. In the left pane, click the [+] net, and then find samba.dsl and click it. Click the Install Selected button at the top of the window.

Open up an SSH session to Xdsl. Enter the following commands, but you may need to change WORKGROUP to MSHOME. These commands backup your Samba configuration file, then change the workgroup, and finally add the E, F, and G partitions as shares. Copy the entire contents of the following, then right-click in Putty to paste them.

cp /opt/samba/smb.conf /opt/samba/smb.conf.bak
sed -i -e 's/^\tworkgroup = .*$/\tworkgroup = WORKGROUP/' /opt/samba/smb.conf
sed -i -e 's/;  encrypt passwords/encrypt passwords/' /opt/samba/smb.conf
printf "[E]\n   path = /mnt/hda50\n   public = yes\n   writable = yes\n\n[F]\n   path = /mnt/hda55\n   public = yes\n   writable = yes\n\n[G]\n   path = /mnt/hda56\n   public = yes\n   writable = yes\n\n" >> /opt/samba/smb.conf
printf "mount /dev/hda55\nmount /dev/hda56\nnmbd -D -s /opt/samba/smb.conf\nsmbd -D -s /opt/samba/smb.conf\n" >> /opt/bootlocal.sh

Now you need to mount the other partitions, restart Samba, and set a new Samba password for root. Just hit enter twice when it asks for a password to use a blank password.

mount /dev/hda55
mount /dev/hda56
nmbd -D -s /opt/samba/smb.conf
smbd -D -s /opt/samba/smb.conf
sudo smbpasswd -a root

Wiki Parser v0.1 – Read Wikipedia Offline

Wiki Parser is a Python program which takes a Wikipedia dump and hosts it locally, offline. To save disk space, Wiki Parser keeps the dump compressed and serves articles to a web browser without decompressing the entire file. I originally created it to serve up Wikipedia articles from a compressed version, but seek times on the compressed file were too great and the program really only works on smaller Wikipedias such as the Simple English Wikipedia.

Download the latest dump of Simple English Wikipedia here
Download Wiki Parser v0.1 here

microdc2 for Windows/Cygwin

microdc2 (http://corsair626.no-ip.org/microdc/) is a command-line Direct Connect client originally developed for flavors of Linux. This is a version compiled for Windows/Cygwin and requires a Cygwin environment to run. This program provides a command line version of Direct Connect on Windows which is especially useful if you run an SSHd server with GNU Screen on Windows and want to be able to remotely manage a Direct Connect client. Read the README.WIN.TXT for installation instructions and read the README for usage instructions.

Download microdc2 v0.15.6 for Windows/Cygwin here

microdc2

Using Twisted Python library to replicate packets

Most of the programming I do is packet replication and automation. My workflow is to first analyze the handshakes and protocols in FireFox with the Tamper Data add-on. I use WireShark to nail down the interaction details. Finally, I recreate everything using the Twisted library in Python.

Here’s a boilerplate to quickly recreate packets using Twisted. It requests and saves a copy of Google.com. Simply replace with your favorite URL, or un-comment the two lines and comment the other getPage to send a POST request.

from twisted.web import client
from twisted.internet import reactor
 
agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12 (.NET CLR 3.5.30729)"
headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language':'en-us,en;q=0.5', 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7'}
headers_post = headers.copy()
headers_post['Content-Type'] = 'application/x-www-form-urlencoded'
 
def success(data):
    print "Success!"
    f = open('save.html','w')
    f.write(data)
    f.close()
    reactor.stop()
 
def failure(fail):
    print fail.getErrorMessage()
    reactor.stop()
 
def request():
    url = "http://google.com/"
    d = client.getPage(url, headers=headers, agent=agent)
    #post_data = ""
    #d = client.getPage(url, postdata=post_data, method="POST", headers=headers_post, agent=agent)
    d.addCallback(success).addErrback(failure)
 
request()
reactor.run()

To make it continually request the same page, change the success method to this:

def success(data):
    print "Success!"
    f = open('save.html','w')
    f.write(data)
    f.close()
    reactor.callLater(0.5, request)

Rewind

I got an email today:

Hello,

Unfortunately but your data is damaged un-fixable

Thank you,
www.000webhost.com

Data damaged un-fixable! But I’m smart; I make backups. I found the file in my backup folder, extracted the SQL, then opened it to verify everything was fine. I only make backups every few months, but I had made one this month on the July 7th. When I do backups I backup all my websites and carefully label them and file them away on one of my hard drives, then I burn a copy of my entire backup folder to a DVD, just in case my backup hard drive dies. Sadly, this past month when I was making backups, I mislabeled one. Wouldn’t you know it had to be the one with data damaged un-fixable. The SQL file belonged to a different site, and my blog’s data really is gone.

Google cache has some of the old posts and I’ll work on moving the ones that matter which are really only two posts. This backup is from November 2008. Not much lost since I haven’t been active lately, but data loss is never fun.