How to send messages using Python

In this tutorial, we will learn to send message using Python. We can use this approach in building new Python applications.

Tools we will be needing :

  1. Python – Version 2 or 3.
  2. Virtual Environment  – To create isolated Python environments
  3. Twilio Trial Account.
  4. Twilio Python helper library.

Twilio API :

We will be using Twilio Python helper library to send messages over the phone. To use Twilio library we will need to create twilio account. Signup for a free account

Twilio   Try Twilio Free.png

Once you had signed up on twilio, you will receive verification code on your registered phone number, input this code on twilio verification page and your account is set to use twilio services.

In Trial Mode, Twilio can be only used to send messages to a validated phone number. You will have to upgrade your account in-order to send messages to any phone number.

Now login into twilio and grab twilio account test credentials by click over here.

#Note : Copy your TEST ACCOUNT SID and TEST AUTHTOKEN, we will be using this in python script.

Install Python Dependency

As mentioned earlier that we will be using twilio helper library for our Python application. The library can be install using pip command.

We will start the basic Python Project by first creating virtual environment. Virtual environment are used to isolate different Python environments.

virtualenv sendMsg

Now activate this environment using,

source sendMsg/bin/activate

As we are ready with our virtual environment, go ahead and install twilio library.

pip install twilio

Sending Message from Python Script

Create a new python file i.e and enter the following code in the file.

# import the Twilio client from the dependency
from import Client

# put your twilio credentials here
account_sid = "{{your_account_sid}}" # get it from
auth_token = "{{your_authentication_code}}"

client = Client(account_sid, auth_token)

media_url = ""

response = client.messages.create(
    to= "{{enter_your_twillio_account_phone_number}}", #
    from_= "+15005550006", #+15005550006 will be used for trial account.
    body= "Hello, how you doing ?",
    media_url= media_url # if you need to attach multimedia to your message, else remove this parameter.

Now run the python script using


In a few seconds you should be able to see a message appear on your phone – note that messages can sometime take a little longer because we are using test credentials and it might in queued stage. Always check API response to know exact status.

That is everything needed to send messages to phone. Awesome results using few lines of Python code. This code can be added to any Python program to add message sending capabilities.


Create Pycharm Launcher in Ubuntu

Quick way to create Pycharm launcher. 

  1. Start Pycharm using the ./ file located under bin folder.
  2. Once the Pycharm application loads, navigate to tools menu and select “Create Desktop Entry..”
  3. This type of window will open up. Selection_500
  4. Check the box if you want the launcher for all users.
  5. If you Check the box i.e “Create entry for all users”, you will be asked for your password.
  6. A message should appear informing you that it was successful.
  7. Now Restart Pycharm application and you will find Pycharm in Unity dash and Application launcher.

Deploy Django Project Using NGINX and Gunicorn

Meet Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

In this guide, we will try how to install and configure some components on Ubuntu 14.04 to support and serve Django applications.


NGINX accelerates content and application delivery, improves security, facilitates availability and scalability for the busiest web sites on the Internet.

Meet Gunicorn

Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy. This Gunicorn will serve as an interface to our application, translating client requests in HTTP to Python calls that our application can process.

Things we will try to achieve :

  1. Installing Django within a virtual environment.
  2. Start Django project from scratch.
  3. Host this Django project using Nginx and Gunicorn.

Installing Django within a virtual environment:

In-order to have django within virtual environment we need to execute virtualenv command. We can install this with pip.

For Python 2:

sudo pip install virtualenv

Python 3:

sudo pip3 install virtualenv

As now we have our virtualenv ready, we can go ahead and setup projects directory. 

  • mkdir ~/myproject
  • cd ~/myproject

Within this project directory, create a Python virtual environment by typing:

virtualenv myproject_env

By executing this command “myproject_env” named directory will be created. It will be used to install and configure different python environments.

Now to activate this virtual environment, execute following command.

source myproject_env/bin/activate

With your virtual environment activated, We can move forward by installing Django, Gunicorn.

pip install django gunicorn

Components which are installed under virtual environment are isolated from other os global environments.

Start Django project from scratch.

Now with virtual environment been activated, Start django project using django management command. startproject myproject .

Now open fiile located under myproject folder.

Edit this by replacing following code :

ALLOWED_HOSTS = ['your_server_domain_or_IP',]

STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Complete remaining basic project setup

cd ~/myproject

./ makemigrations

./ migrate

./ createsuperuser

./ runserver
In web browser, enter your_ip_address:8000:

Once you had finished exploring recently created project, hit CNRL + C.

Now let’s test Gunicorn interface.

cd ~/myproject

gunicorn --bind myproject.wsgi:application

This will start gunicorn on same interface on which our earlier development server was working.

Now type this command to deactivate the virtual environment:


Now we have to create Gunicorn systemd service file.

This service file will be used to start or stop our application server.

Open the gunicorn systemd service file using this command.

sudo vi /etc/systemd/system/gunicorn.service

Edit this service file with following data.

Description=gunicorn daemon

ExecStart=/home/vivek/myproject/myproject_env/bin/gunicorn --workers 3 --bind unix:/home/vivek/myproject/myproject.sock myproject.wsgi:application


Save and close the file.

Start the Gunicorn service using following command.

sudo systemctl start gunicorn

sudo systemctl enable gunicorn

This will create socket file inside your project directory.

Always check that the socket file has www-data group and this group should have group ownership.

Now is the time to configure NGINX

Create new server block in Nginx’s sites-available directory.

sudo vi /etc/nginx/sites-available/myproject

Now edit this server block with following code.

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/vivek/myproject;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/vivek/myproject/myproject.sock;

Save and close the file.

Now go forward by enabling the file by linking it to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Test your Nginx configuration for syntax errors by typing:

sudo nginx -t

If no errors is found, Restart Nginx by typing:

sudo systemctl restart nginx

In the end, Following points were achieved :

  1. Virtual environment having its own Django project.

  2. Django can now handle client requests thanks to Gunicorn.

  3. Nginx is added to act as a reverse proxy to handle client connections and serve the project.