Python Sample Programs

  1. [Source] [Run] Hello World and command line arguments
  2. [Source] [Run] Strings samples
  3. [Source] [Run] Dates
  4. [Source] [Run] if, for, while: branching and loops
  5. [Source] [Run] Arrays
  6. [Source] [Run] Functions
  7. [Source] [Run] Files, Directories
  8. [Source] User prompt. No [Run] for this sample because it requires user prompted input.
  9. [Source] [Run] Encryption and Decryption, Base64 encoding and decoding

Twilio SMS Sample Programs

  1. [Source] Send an SMS message.
  2. [Source] List SMS messages.
  3. [Source] Delete an SMS message.
  4. [Source] Send a pre-recorded voice message.

Set Up

Installing Python on Windows

  1. Go to the Python Download page: download link.
  2. I clicked the download link for most up to date 2.7 Python version, example: Python 2.7.12.
    Note, the reason I selected 2.7, is that there are more samples available online.
  3. I downloaded the file for Windows x86 MSI installer: python-2.7.12.msi.
  4. Run the downloaded file and install Python.
  5. Test by running the command python from a Windows Command Prompt window.
    C:\Users\root> python
    Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    Exit Python:
    >> exit()

Add Python support to the NetBeans IDE

  1. From the NetBeans Tools/Plugins window, install Python.
    Now Python projects can be created with Python syntax aware code development.
  2. In my environment, I run a Java web server program that I wrote. The web server runs Python programs using a method similar to CGI. This allows me to develop web applications, using Python programs, that port to my Dreamhost online web server: TigerFarmPress.com.

On the Dreamhost Webhosting servers

Python 2.7.x is already installed.

  1. I logged into the server using SSH, using the Putty client on my Windows computer.
  2. Test by running the python command:
    $ python
    Python 2.7.3 (default, Jun 22 2015, 19:33:41)
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    Exit Python:
    >> exit()
  3. I created a basic script to run my Python programs: /home/.../tigerfarmpress.com/python/run
    #!/bin/sh
    
    # ------------------------------------------------------------------------
    # Make CGI query strings available to the Python scripts:
    IFS="${IFS}&"
    set $QUERY_STRING
    Args="$*"
    
    echo "Content-type: text/html"
    echo
    echo "<pre>"
    python /home/.../tigerfarmpress.com/tech/booksJava/projects/PythonHello/src/$1 $2 $3 $4 $5
    echo "</pre>"
  4. I uploaded my Windows sample programs to the directory: /home/.../tigerfarmpress.com/tech/booksJava/projects/PythonHello/src/
  5. Test the CGI script by running the Hello World program: http://tigerfarmpress.com/python/run?hello.py.

Add Twilio Python Library support to Dreamhost

  1. To install the Twilio library, first setup the Dreamhost environment by following their instructions at this link.
    Commands I use:
    $ pip install virtualenv
    $ cd /home/.../tigerfarmpress.com/env
    $ virtualenv .
    $ 
    The Twilio Python Library can now be installed and used.
  2. I downloaded the library zip file (download link) to my Windows computer, then uploaded the file to the Dreamhost server into a work directory.
    Twilio instructions to install The Twilio Python Helper Library.
  3. In the work directory, I ran the command to activate the virutalenv, then ran the install command:
    $ cd <work directory>
    $ source $HOME/tigerfarmpress.com/env/bin/activate
    $ python setup.py install
    $ 
    The Twilio Python Library is now ready to use.
  4. I created a basic script to run my Python programs using the Twilio Python library:
    /home/.../tigerfarmpress.com/python/run
    #!/bin/sh
    
    # ------------------------------------------------------------------------
    # Set environment
    VIRTUAL_ENV="/home/.../tigerfarmpress.com/env"
    export VIRTUAL_ENV
    PATH="$VIRTUAL_ENV/bin:$PATH"
    export PATH
    # ------------------------------------------------------------------------
    # Make CGI query strings available to the Python scripts:
    IFS="${IFS}&"
    set $QUERY_STRING
    Args="$*"
    # ------------------------------------------------------------------------
    echo "Content-type: text/html"
    echo
    echo "<pre>"
    python /home/.../tigerfarmpress.com/tech/booksJava/projects/twilioPython/src/$1 $2 $3 $4 $5
    echo "</pre>"
  5. I developed and tested Twilio Python scripts on my Windows computer.
    I uploaded the scripts into the directory:
    /home/.../tigerfarmpress.com/tech/booksJava/projects/twilioPython/src/
    I then ran the programs using an HTTP url on my browser.
    Note, my Twilio sample programs are listed above.