Skip to content

Python

This section shows sample usage in various environments.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import sys
import httplib
import urllib
import base64

URL = "smartreplypath.spirius.com"
PORT = 55001
CREDENTIALS = base64.b64encode("Username:Password")

def send_replypath_sms_v1(dest, message):
  try:
    encoded_dest = urllib.quote_plus(dest)
    encoded_mess = urllib.quote_plus(message)
    h = httplib.HTTPSConnection(URL, PORT, timeout=10)
    query = "/v1/replypath/sendsms/%s/%s" % (encoded_dest, encoded_mess)
    h.putrequest("GET", query)
    h.putheader("Accept", "application/json")
    h.putheader("Authorization", "Basic " + CREDENTIALS)
    h.endheaders()
    r = h.getresponse()
    if (r.status != 200):
        return ("%d %s") % (r.status, r.reason)  

    return r.read()

  except Exception, e:
    sys.stderr.write(str(e))

  finally:
    if (h is not None):
        h.close()

print send_replypath_sms_v1("+46123456789", "Hello world")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import sys
import httplib
import urllib
import urllib2
import base64

URL = "https://smartreplypath.spirius.com:55001/v1/replypath/sendsms?"
CREDENTIALS = base64.b64encode("Username:Password")

def send_replypath_sms_v2(dest, message):
  try:
    params = { 'To': dest, 'Msg': message }
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(URL + encoded_params)
    request.add_header("Authorization", "Basic %s" % CREDENTIALS)
    response = urllib2.urlopen(request)
    return  response.read()

  except urllib2.HTTPError, error:
    return error.read()

  except Exception, e:
    sys.stderr.write(str(e))
    return ""


print send_replypath_sms_v2("+46123456789", "Hello world")

This example uses the third party library Requests, which makes sending HTTP requests easier.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import sys
import requests

URL = "https://smartreplypath.spirius.com:55001/v1/replypath/sendsms"
USER = "Username"
PASS = "Password"

def send_replypath_sms_v3(dest, message):
  try:
    _params = { 'To': dest, 'Msg': message }
    r = requests.get(URL, params=_params, auth=(USER,PASS), timeout=10)
    return r.text

  except Exception, e:
    sys.stderr.write(str(e))
    return ""

print send_replypath_sms_v3("+46123456789", "Hello world")