Simple Time-Based routing for FreeSWITCH using Python

May 17th, 2010

Here is a quick and dirty script I wrote that will use python to do Time Based Routing. This is useful for people using a dynamic source of their XML dialplan (such as WikiPBX) who cannot use the normal


from freeswitch import *
import datetime

def input_callback(session, what, obj):

def handler(session, args):

	the_uuid = session.getVariable("uuid")
	now =
	if now.hour >= 16 and now.hour <=21:
		session.execute("ivr", "YourIVRNameHere")
        	session.execute("voicemail", "ProfileName DomainName MailboxName")

Python code snippet: Read from stdin, write to file

November 9th, 2009

I was testing another script that was supposed to take the stdin and email it to me, and that script wasn’t working, so I wanted to verify that data was actually being passed into stdin in the first place so I could narrow down the problem.

Here is a really simple snippet of code which you can invoke from a command line while piping stdin into it. Example:

cat filename.txt | python

Here is the script itself, along with comments to explain what it’s doing.

import sys
# the next line tells the script to read from stdin and use it for output
output =
#the next line tells the script where to save the output
outfile = open('/tmp/file.txt', 'w')
#the next line tells the script to consider the file as stdout instead of the console
sys.stdout = outfile
#print the output from stdin into the file
print output
#close the file!

This is useful if you are testing any application which sends email automatically using an external sendmail type application. Tell the application to use this script instead of sendmail as the email application for example, and after sending a mail from the program,


will contain the output of whatever the program was trying to email. It is very useful in narrowing down problems from programs which interact with external scripts via pipes. So, enjoy!

It’s the remix, y’all

August 20th, 2009

Hey guys. Not much coding being done lately, gotta love summer. On the other hand, check out these tracks I remixed using Logic Pro and a bunch of acapella tracks from mc chris.

Click here to listen to full length free MP3s of the remixes!

I have a bunch of old tracks up at New Music Canada and also I have a myspace page about it all.

Hope you like listening to them! Contact me if you want me to make you some beats.

Python Code Snippet: Send a file to an FTP server

June 22nd, 2009

Here is a snippet of code I put together to take a file and upload it via FTP to a remote site.

import ftplib
# connect to the server
ftpsite = ftplib.FTP('','FTPUSER','FTPPASSWD')
file = open('FileName.txt','rb')                # file to send
ftpsite.storbinary('STOR FileName.txt', file)   # send the file
file.close()                                    # close file and FTP

There are a crapload of similar code snippets available here: Seb Sauvage’s Python Snyppets. Thanks for putting all of these online, Seb!

Simple Example Usage of MySQLdb with Python

June 18th, 2009

MySQLdb is a set of open source Python libraries that allow you to interact with MySQL databases at a higher level than the traditional C API methods.

Here is some code that takes two command line arguments, and stuffs them into a table as contents of the fields “ID” and “INPUT”.  (in other words it is a simple database of ID numbers and text).

#!/usr/bin/env python
# example of taking data from Python variables and inserting them into a database.
import MySQLdb, sys

id = sys.argv[1]
input = sys.argv[2] 

sql = "INSERT INTO your_table_name (id, input) VALUES("
sql += id
sql += ", '"
sql += input
sql += "')"

db = MySQLdb.connect(host="yourhost" db="yourdb", user="username", passwd="password" )
dbc = db.cursor()


Slowly shaping things up

June 18th, 2009

I’ve been slowly shaping things up.  I added a bunch of wordpress plugins and stuff to help me gauge the site and added some ads (yeah yeah, I know I know) as a way to offset the cost of the site to keep running as we go forward.

I have a bunch more code to post, and I think I’ll post some of it tonight before I sign off.

My first python script – receive a fax via FreeSWITCH / spandsp

June 6th, 2009

Here is the first ever complete python script I wrote.  This was done recently because I wanted a way to receive faxes via FreeSWITCH that I could manipulate and use for multiple fax lines and customers with a minimal amount of overhead.  The script receives the fax using spandsp/mod_fax and then converts the fax image into a PDF file, and emails it to me.

This script is meant to be called from the FreeSWITCH dialplan XML, and requires that you have a recent build of FreeSWITCH, a working mod_fax installation, a working mod_python installation, and a functional ghostscript installation with the “ps2pdf” command.

I’ve tested this script to work on various builds of the May 2009 FreeSWITCH SVN trunk codebase, under FreeBSD 7.x, using a virtual server from the amazing VoIP provider  It works great for receiving faxes in email and has no limitation of how many faxes it can receive at once (fax lines don’t ring busy unless FreeSWITCH runs out of CPU/capacity to keep handling them).  On my Link2VoIP virtual server I was able to simultaneously send and receive 50 faxes to and from myself concurrently without anything crashing.


Once you have a working FreeSWITCH, with all of the above requirements met, create an extension similar to the following, which calls the script when the extension is called:

<extension name="test_rxfax_python"/>
       <condition field="destination_number" expression="^\*90012$">
               <action application="set" data=""/>
               <action application="python" data="process-rxfax"/>
               <action application="hangup"/>

Set the recipient variable in dialplan to the email address you want the fax to get sent to.  The SMTP server specified in the script must be able to relay mail to this email address.

Thanks to the amazing developers of FreeSWITCH and spandsp.

Comments/improvements are appreciated!

Get the script here.

first post!

May 25th, 2009

i’m going to be putting my python code exploits up here for a while, as well as any other random stuff i feel like putting here.  DEAL WITH IT!