Does your favorite web service have a awkward interface? Make your own with Python, Python-Requests and BeautifulSoup!

There are some pretty useful sites out there, but some interfaces are just plain annoying.

Why settle when you can create your own interface to your own liking?

Step 1: Login
First take a look at the source code of the form on the login page:



We will use python-requests to make all our requests with a simulated user session. See for more details.
We will start by passing in all those input values to

By using instead of the plain, we retain all the cookie information necessary to simulate an actual logged in user.

Step 2: Collect the message links
BeautifulSoup makes parsing html extremely simple. See for the docs.
Say we have an html string and we’d like to find all the html elements with the “message” class. Here’s how we would do that with BeautifulSoup:


In step 1 we logged into and got a response object. We can pass the html contents of this object to BeatifulSoup to begin parsing.
For our case we need the next_page link and the links to the messages (the html code of POF is terrible, so some hackery was necessary to get the elements properly):soup = BeautifulSoup(response.text)

Step 3: Collect the content
We now need to go to each link and fetch the message content and user data.
Continuing to use the session object for all requests, we get:



Step 4: Pretty Print the data
I have opted to use Jinja2 to render the html, but this is not at all necessary. Jinja2 is a simple templating library that is used in many python web frameworks. See for a more in depth tutorial.

It’s fairly simple to use:

Be careful to properly encode your strings when using Jinja2. POF has some malformed characters which required cleaning the strings with “”.encode(‘ascii’, ‘ignore’)

Step 5: Run it!
Below is the script in its entirety.


This script can be easily tweaked to be used with your favorite service provider.

Push notifications with Flask, EventSource and Redis

  1. Save the file below as
  2. Run python
  3. Open a bunch of browsers pointing to http:localhost:5000/
  4. Open up each browser’s console window.
  5. Hit submit in one, the rest should update.


You need to have Flask and redis installed:


Post to Facebook on Behalf of Your User with Python/Django

Posting on facebook through python is fairly simple: you need acquire your user’s facebook access token, which you use to access the users facebook wall.

Step 1: Create the facebook app
First, create a new facebook app on and note down the APP_ID and APP_SECRET. Store this information in your django file.

Step 2: Acquire an oauth code
To acquire an oauth code, simply redirect your user to the following url: along with these arguments:

We get:

Step 3: Handle the redirect from facebook
Upon successful signin and approval of your user, facebook will redirect back to the ‘redirect_uri’ used above, with an additional ‘code’ argument in the following format:

We can now use this code to acquire the access_token:


Step 4: Final step, use the access token to share on the facebook wall
Now that we have the access token saved, we can update the users status by sending a post request to


Make sure to import urllib, urlparse and requests when using the above code. The first two are built into python but requests can be installed with:

Crop your images with CSS!

Instead of tracking down your language’s fancy image libraries, you can crop images very quickly with css.

Simply use negative absolute positioning!






If you don’t know the size of the image beforehand, you can use some javascript to compute the height and width:


Setting up iOS Push Notifications (APNS) with Python/Django through pyapns

Working with iOS for the first time can be a bit frustrating. For me, the most frustrating part was working with

Here I hope to help make push notifications with Python as painless as possible.

Step 1: Enable for Apple Push Notification service and generate a certificate

  1. To get started, log in to you developer account.
  2. Click on iOS Provisioning Portal
  3. Click on App IDs
  4. If you already have an app id, click on “configure” otherwise create a new one, and then, click “configure”.
  5. Check “Enable for Apple Push Notification service”, then click on the “Configure button”, under “Development Push SSL” Certificate.
  6. Launch Keychain Access  (Command + Space + “Keychain Access”)
  7. Within the Keychain Access drop down menu, select Certificate Assistant, then select Request a Certificate from a Certificate Authority
  8. In the Certificate Information window, enter the following information:
    1. In the User Email Address field, enter your email address
    2. In the Common Name field, create a name for your private key
    3. In the Request is group, select the “Saved to disk” option
    4. Click Continue within Keychain Access to complete the CSR generating process
    5. Upload the CSR file you just created in the new dialog window.
  9. Apple will now generate an SSL Certificate, for you to download. Download and open the newly generated certifcate.
  10. This should open up Keychain Assistant, in which you will see “Apple Development Push Services …”; right click on this item and select to “Export”. Save as “apns-dev-cert.p12″.
  11. Now, expand ”Apple Development Push Services …”. Right click on the private key. Export this one as well, as “apns-dev-key.p12″.

Step 2: Convert the certificate to PEM format

  1. Load up Terminal (Command + Space + “Terminal”), and cd into the directory where you saved your *.p12 files.
  2. Convert the p12 fiels to PEM format with:
  3. Remove the password with:
  4. Combine the key and cert files into apns-dev.pem, which we will use when connecting to APNS with Python:
  5. Upload apns-dev.pem to your server where you will be sending out push notifications from.

Step 3: Set up your iOS app to receive notifications

  1. In your AppDelegate.m file, override the following methods:

    The first method enables push notifications; the second one sends each device’s unique device token to your django app. Make sure to update the url to your django server.

Step 4: Add the necessary code to your Django app to store new iOS device tokens

  1. Add the following model to your file:

  2. Add the following url route to your Django urls file:
  3. Add the following to your file:



Step 5: Set up pyapns

  1. Install pyapns with:
  2. Ensure that pyapns is properly installed by loading up the python interpreter and running:

  3. Start the pyapns server (you should also add this to your server startup script):

  4. Create a file in your project, if it doesn’t already exist, and add the following class into it:

  5. Add the correct APNS variables to your file:
  6. Next in the file where you would like to fire push notifications add the following:
  7. Now just call send_notifications(“your lovely message”). This should make all your iOS devices magically receive the message! Note that only those devices whose serial numbers have been added to the development provisioning profile will receive messages.

Step 6: Enjoy!

How to Make a Bootable Linux USB on Mac OS X

I’ve tried using disk utility and didn’t seem to get anywhere. Using linux’s dd command seems to work best.

Step 1.
Open up the Terminal app and enter in:
diskutil list
Figure out what your usb device is called – mine was called /dev/disk1.

Step 2.
Unmount the disk:
diskutil unmountDisk /dev/disk1

Step 3.
Copy the iso to the USB:
dd if=image.iso of=/dev/disk1 bs=8192

Step 4.
Eject the disk:
diskutil eject /dev/disk1

Threading Example in Python

Running multiple threads in Python is actually quite simple:




Painless Django Deployments

Instead of constantly changing for different environment, automate the the process with some simple python:


The Python or Operator Shortcut

If statements are one of the most basic concepts used in all programming languages. Unfortunately they also make code ugly, unreadable, and sometimes even inefficient. Check out this google talk on if statements:

Using the or operator we can avoid ifs when checking for null values before assignment:

Traditional if:

Using the or operator: