Posts Tagged ‘python’

Python Code Snippet: Send a file to an FTP server

Monday, 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('ftpserver.address.com','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
ftpsite.quit()

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

Thursday, 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()
dbc.execute(sql)

It’s JUST THAT EASY! :D

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

Saturday, 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 Link2VoIP.com.  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.

Instructions:

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="recipient=YOU@YOURDOMAINHERE.com"/>
               <action application="python" data="process-rxfax"/>
               <action application="hangup"/>
       </condition>
</extension>

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.