Python

We offer code samples to make it easy for you to plug in SMS functionality into your applications.

Code Sample

#!/usr/bin/env python

import httplib

def sendSMS(username, password,destination, message):
        body = “<?xml version=\”1.0\” encoding=\”UTF-8\” ?>”+\
               “<Request xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”” +\
   :noNamespaceSchemaLocation=\”http://schema.2sms.us/1.0/0410_RequestSendMessage.xsd\”   ” +\
        “Version=\”1.0\”><Identification>” +\
        “<UserID>” + username +”</UserID>” +\
        “<Password>”+password+”</Password>” +\
        “</Identification>” +\
        “<Service>” +\
        “<ServiceName>SendMessage</ServiceName>” +\
        “<ServiceDetail>” +\
        “<SingleMessage>” +\
        “<Destination>”+destination+”</Destination>” +\
        “<Text>”+message+”</Text>” +\
        “</SingleMessage>” +\
        “</ServiceDetail>” +\
        “</Service>” +\
        “</Request>”
    h = httplib.HTTP(‘www.2sms.com’)
    h.putrequest(‘POST’,’/xml/xml.jsp’)
    h.putheader(“Content-type”,”text/xml”)
    h.putheader(‘Content-length’,”%d” % len(body))
    h.putheader(‘Accept’,’*/*’)
    h.putheader(‘Host’,’www.2sms.com’)
    h.endheaders()
    h.send(body)
    reply, msg, hdrs = h.getreply()
    if reply != 200:
         raise RuntimeError(‘HTTP request error’,(reply,msg,hdrs))
    else:
    print “HTTP request to 2sms OK”

username = ‘username@example.com’
password = ‘password’
destination = raw_input(“Enter a destination Mobile: “)
message = raw_input(“Type your message: “)
sendSMS(username,password,destination,message)
### pause for Return key (so window doesn’t disappear)
raw_input(‘press Return to exit>’)

Scroll to Top