Python Check for 301 redirects

In web development, moving from staging to production servers, it’s useful to check that staging is pointing to production after the DNS changes.  Simple to check all urls if the site you’re working on only has few pages. A simple curl -I http://your.url.here.com will work just fine.

But what if you have a hundred or more pages to check? If I was paid by the hour, yeah I would copy/paste each url into a terminal and check, but I’m salary. So here’s a quick python script to check them for me.

You need to provide it a text file with all the urls on their own line. Easy if the urls are in a spread sheet, simple copy/paste into the text file and off to the races. This code will account for white-space and empty lines, however, it will not account for missing http/https prepends.  So far testing shows compatible with Python2 and Python3. Requires the requests library.

# requires requests lib
# - pip install requests
import requests
import sys
def redirectTest(url):
    with open("no_redirects.txt", "a") as no_redirect:
        try:
            r = requests.head(url, allow_redirects=False)
            if (r.status_code == 301):
                print("+ %s :: %d" % (url, r.status_code))
            else:
                print("- WARNING: %s :: %d" % (url, r.status_code))
                no_redirect.write("%s :: %d\n" % (url, r.status_code))
        except requests.exceptions.RequestException as e:
            print("! Error with request: %s :: %s" % (url, e))
def load_urls(urlfile):
    # clean urls of white space and remove empty lines
    # this does not prepend http/https to urls missing them
    clean_urls = []
    with open(urlfile) as f:
        urllist = f.readlines()
    for i in urllist:
        i = i.strip()
        if i:
            clean_urls.append(i)
    return (clean_urls)
args = sys.argv
if len(args) < 2:
    print("! Error: Missing arguments.\n\nUsage: python check301.py urls.txt")
else:
    try:
        l = load_urls(args[1])
        for i in l:
            redirectTest(i)
    except Exception as e:
        print("Something went wrong: %s" % e)

Feel free to change, modify, use, extend, and improve this code. You can find it on GitHub here.

This link will 301 to my About page.

Android Twisted SpyCam

I have been playing around with Androids ASE(android scripting environment). ASE allows you to run scripts on your android powered device. You can use Python, PHP, Javascript, Pearl, Ruby and much more. To install ASE on your device you must either have root, allow third party apps install,, or know how to sideload apk’s using android’s SDK tools like adb.

Continue reading “Android Twisted SpyCam”

Fun With MindFlex

For this valentines day, my wife got me a MindFlex from Mattel. I have been wanting one of these since they have been on the market. It is a really interesting game where you control a foam ball with your mind. The ball will rise with the more focused you are. Letting your mind wonder and as Morphius likes to say “free your mind”, the ball will lower.

Continue reading “Fun With MindFlex”

Android on IRC

I recently gave a lecture on DDoS in my Network OS class. I demonstrated a simple bot on the computers in the class. I also demonstrated how portable devices are capable of running bots as well.

I used Android Scripting Environment (ASE) for the demo. The commands I gave it where text, call, speak aloud, and vibrate. The only draw back ASE has is no real GUI support. I can’t wait for the day when OpenGL ES is ported to python. Anyway, ASE allows access to almost all of the phones featurtes. Here is a link to the API ref: http://code.google.com/p/android-scripting/wiki/ApiReference

Continue reading “Android on IRC”

Maverick Mouse

NOTE: Better to watch the video on youtube and viewed full screen.

In this project I take a Nerf Maverick and use it as my mouse for my laptop. This was easier than I thought it would be. It took about two days to do, and I use python for the computer interface. This is much like the Nerf gun for the Wii. You could do the same thing with the gun for the Wii and connect your Wiimote to the PC via bluetooth. That way uses PyGlove as the emulator.

Continue reading “Maverick Mouse”

Facial Recognition via Python (non-OpenCV)

After working with the IR, I decided to cut the IR equipment out of the equation and was going to try my hand at facial recognition. This one does not use OpenCV or haarcascades. Instead, it uses a library I found through google called fdlib. It was written in C/C++, so using it with python shouldn’t be a problem. You can embed C/C++ in python via ctypes. You have already seen me use it in a previews post to set the cursor position in windows.

Continue reading “Facial Recognition via Python (non-OpenCV)”