Python, not PHP

The first few posts of this blog dealt with PHP and Zend Framework. I am now leaving PHP behind. I am going to focus on using Python and Django (probably a few others: Flask and web.py look great for small sites) from now on.

This leads to a question: why?

First, I have dabbled in Python and liked it. I had created a few simple Google App Engine sites. I used webapp (an App Engine framework) to build them. Webapp is a very simple framework with which you can easily map the route to a HTTP verb and tell it which function to run. I was able to get something running with very little trouble, despite the fact I knew almost nothing about Python.

Secondly, I have heard many good things about Django. I am always reading blog posts about different frameworks just to see what ideas/methods they have. Django looked great so I decided to use it for a small project I had.

On a side note, the PHP framework Laravel looks great. I have not used it and do not plan to, but if I were to develop something in PHP I would use Laravel.

Was this quick? I believe so. Was it dirty? Certainly not.

Finally a blog post kicked my butt and took every excuse I could make and silenced them – Quick doesn’t have to mean dirty. The author live blogs building a guest book app in Flask. I do not want to be incendiary, but Python just felt more correct. I did not see a need for a lot of boilerplate. There is no web server set-up. I did not have to spend time trying to figure out if you have the correct extensions and correct INI files. I agree with the author by the end: “Was this quick? I believe so. Was it dirty? Certainly not.”

I also have to comment about another post Alex (veekun) wrote. I do not see how this is not incendiary, but here it is: PHP: a fractal of bad design. I have not been able to work on a PHP project since reading this post. I was already moving away from PHP, but I was still using it for a few projects. After reading this I decided to go cold turkey on PHP.

This is not an opening shot to a language flame war. I look forward to posting a lot more Python and Django content.

WebRTC – Using your Webcam for video in HTML Tutorial

Download the src(github)
View the demo

WebRTC is project to bring Real Time Communications to the browser. It is designed to allow video/voice chat with the browser without plugins. Because you technically have to have access to the webcam to do video chat, WebRTC gives you a javascript API to the webcam. This is what I am going to show you how to use. I am not going to go into the audio portion or how to create a real time communication link. WebRTC is still very much in beta and most browsers use a vendor prefix to get access to the webcam. I will show you what I did to create my webcam javascript abstraction library.

Create a Webcam Library

I created a small shim/abstraction library to make getting and using the webcam easier. I used what I learned from creating CreateMyPhotobooth (this is a small plug, but I am relaying what I learned from this). I have the project on github with a demo, grunt build info, and tests.

Use of the Library

When you include the library you will have access to the wcvj (WebCamVidJa) object. The first function is videoIsSupported. Continue reading “WebRTC – Using your Webcam for video in HTML Tutorial”

Google Webmaster or Blitz.io Authentication with Django

This will be just a quick post. Sometimes you have to prove your ownership of a domain. This is true when you sign up for Google Web Master Tools or want to run a test with Blitz.io. Google and Blitz.io give you a long hexadecimal string that you have to respond to from your site.

In the case of Blitz.io they give you a string that needs to respond with ’42’. If you are running django you can do this very quickly and easily. In the main project folder in the urls.py file:

#import HttpResponse
from django.http import HttpResponse

#in the url list add
url(r'^mu-your_hexadecimal_number', lambda r: HttpResponse('42')),

That’s it. I know this kind of breaks separating out views, but this is usually a one time thing. After verifying you can remove this line. I do not really see any sense in creating a view to return two characters.

Amazon S3 Bucket Permissions and s3cmd

I recently moved this website to run in the cloud. It was previously running on an old computer in my basement. I have had plans to play with EC2 machines. I also needed to update my Ubuntu install since it was 10.04. One more mark for moving the server to the cloud column was that I switched ISPs. AT&T was my ISP for the last 3 years, but Comcast had wooed me in with a deal. I was experiencing some intermittent slowness and connection drops with AT&T, so I decided to switch over to Comcast.

One thing that I did not know when I switched was that Comcast does not allow any hosting. I noticed a few days after switching that my site was not up. At first I thought it may have been the router as I replaced that when I changed ISPs. After a little research I discovered Comcast blocks all ports coming in. All of these lead me to the epiphany that now is the perfect time to move the site onto EC2.

Most of Amazon’s AWS documentation is terse and not definitive in any way.

Amazon S3 and s3cmd

I am not really going to go into how to setup and run an EC2 instance. There is a lot of information on the internet about how to do this. Another reason is that EC2 instance management does not have any definitive answers as each situation will require different solutions. I will note that for a simple setup (one server) can be done completely with the web interface. Most tutorials will have you use the EC2 command line tools.

I will talk about S3 and backing up your data with s3cmd. Backing up your data is similar to flossing. You are always told you need to do it, but it is very easy to skip. It also does not affect you until you have a teeth cleaning or your server goes down. I did not have this problem as I had good backups that made moving my server to the cloud very easy.

The old server was using Dropbox as a poor man’s cloud sync. I did not run it as a daemon, I would let it run in the background. This was not a huge issue as I only rebooted the server once or twice a year. I decided to change that to syncing to S3.

Backup Scripts

Linux is really easy to backup. Most configs are text files, so all you have to do is tar them up. All that I backup is:

  • sql dump
  • tar of the folder that held the website
  • tar of etc
  • a list of installed packages (dpkg –get-selections)

Cron these up and you have a disaster ready backup plan.

s3cmd

S3cmd is tool that allows you to get and put data on S3. I know that Ubuntu 12.04 does have it in the repository, so installation is easy. Configuring it is a little more difficult. Well I should say that configuring S3 with the correct permissions is more difficult.

You can easily just use your Amazon AWS user and give it full permissions, but you should have a separate user. First thing is to create a user in IAM (AWS dentity and Access Management). Make sure you copy the Access Key ID and secret. If you do not copy this you cannot get the secret again. You can create a new one, but each user can only have two credentials (you can delete old ones).

This is where things get difficult. There is no documentation that clearly states what permissions you should add. Here are the permissions I finally went with that allows s3cmd to work:

{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": ["arn:aws:s3:::[your bucket]/*"]
    },
    {
       "Effect": "Allow",
       "Action": "s3:ListAllMyBuckets",
       "Resource": "*",
       "Condition": {}
    }
  ]
}

Please note that these are attached to the user. There are two permissions here. The first one allows the user to do everything inside of that bucket. The other allows the user to list all the buckets.

S3cmd will now work. You tell it to put your files on S3, s3cmd put s3://[bucket name]/file_name. That’s it. My current backups only cost me around $.10 a month. There’s really no reason to not be backing up your server to the cloud.

New Posts coming soon

I have fallen behind writing posts through Christmas and the new year. I plan on making more posts and github repos. I can tell by the traffic and starring/forking of my repos that these posts at least help a few people out there.  Now that I have stated my excuses, a saying I have heard is that excuses only make you feel better, is to lay out a road map of my next few posts. This will also force me to actually make good on these statements.

Some things I will get to:

  • Django – I have found a new framework that I like. Unfortunately that means that I will most likely not be doing many more Zend Framework posts.
  • Facebook – Because I have been using Django I had to write some code to access Facebook.
  • WebRTC – Otherwise known as webcam through javascript. I will have a post about how to use it.
  • Amazon Web Services – I recently moved my website to be hosted on EC2. I will make a post or two about that.

You can see that even though I have not been making posts, I have been busy.

To finish off the post here is a photo of my dog Gizmo:

Gizmo
Gizmo

Facebook and FourSquare API checkin calls with SVG in HTML tutorial

Download the src(github)
View the demo

This has been an idea in my head for awhile now. The app will get your checkins from Facebook and FourSquare and will show you on a map which states you have been to. It is very similar to the other apps I have created. Although I do use a new skill, manipulating SVGs.

I want to give a quick thanks to Theshibboleth who created the SVG of the US. I downloaded it from Wikipedia.

The SVG

SVG stands for Scalable Vector Graphics. The simplest definition is that it is an XML file that describes how to build an image. If you would like to know more as always you can use Wikipedia or do a quick Google search. SVG has support in HTML5 for inline and loading. I am using inline SVG for this project. To do that all you have to do is copy out all the XML from the SVG file and paste it into your page. This lets you treat the SVG just like HTML. By that I mean that you can apply CSS classes to change the look and use javascript to attach event listeners and/or modify attributes on the fly. Continue reading “Facebook and FourSquare API checkin calls with SVG in HTML tutorial”