How to send SMS from Python using HTTP request

Download: python-sms-api.py (14,5 Mb)

In this guide you will learn how to send SMS messages from Python using HTTP request with the help of Ozeki NG SMS Gateway. Please also find the explanation on its use and the sample source code.

Introduction

HTTP is a very simple but effective method to send data from one application to another. As Python has the ability to calls to submit HTTP requests, it is an excellent mean to send SMS text messages to mobile phones. To adopt SMS functionality to your system you need a stable SMS gateway such as Ozeki NG SMS Gateway. With this software product you can operate with SMS messages with the use of HTTP GET or HTTP POST method calls (Figure 1).

sms sending from python sms api through http
Figure 1 - How to send SMS messages from Python SMS API through HTTP

After the installation, this solution will work as follows: to be able to send SMS messages to mobile phones you need to pass your messages to the SMS Gateway. The built-in webserver of the gateway provides an HTTP SMS API that allows to submit messages. After the messages arrived at the SMS gateway they will be forwarded to the mobile network either with a GSM modem attached to the computer with a data cable, or the gateway directly connects to the SMS center of the mobile service provider over the Internet.

Prerequisites

To setup and operate this solution properly you need the follows:

Configuration

To send SMS messages from Python using HTTP requests, first you need to download, install and configure Ozeki NG SMS Gateway software to your computer. Then import the source code provided below into a new project you write in Python. After you imported it you can configure the code in the following way: In "host" variable replace 127.0.0.1. to the IP address on which Ozeki NG SMS Gateway has been installed. (In our example it is on the local computer that is why its IP address is 127.0.0.1. Replace "user_name" and "user_password" to the values with which you log into Ozeki NG SMS Gateway. Then replace the recipient to the intended recipient. In the message body you can type your message. Next, the example will piece together the HTTP request and then query it. Finally, it will verify whether the SMS sending is successful. In case of success you will read: "Message successfully sent", while if there was a problem the following message appears: "Message not sent! Please check your settings". Please note that you need to customize the values in the source code below.

Source code sample

Feel free to use and modify this source code sample!

<###############################################
##   Ozeki NG - SMS Gateway Python example   ##
###############################################

import urllib

###############################################
###            Ozeki NG informations        ###
###############################################

host = "http://127.0.0.1"
user_name = "admin"
user_password = "abc123"
recipient = "+36304080332"
message_body = "Hello World from Python"

###############################################
### Putting together the final HTTP Request ###
###############################################

http_req = host
http_req += ":9501/api?action=sendmessage&username="
http_req += urllib.quote(user_name)
http_req += "&password="
http_req += urllib.quote(user_password)
http_req += "&recipient="
http_req += urllib.quote(recipient)
http_req += "&messagetype=SMS:TEXT&messagedata="
http_req += urllib.quote(message_body)

################################################
####            Sending the message          ###
################################################
get = urllib.urlopen(http_req)
req = get.read()
get.close()

################################################
###        Verifying the response            ###
################################################

if req.find("Message accepted for delivery") > 1:
    print "Message successfully sent"
else:
    print "Message not sent! Please check your settings!"

More information