CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2012
    Posts
    6

    How to send SMS from Python using HTTP request

    Hello. I have recently found a great way to send SMS messages from Python. It turns out, it is easy to send messages from Python using HTTP requestes and a software called Ozeki NG SMS Gateway. I downloaded and configured the software (they have great config info on their website) and then I use the sample source code:
    ###############################################

    ## 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!"

    It was that easy. More useful info on: http://ozekisms.com/index.php?owpn=607
    Have a nice day!
    Jacob

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to send SMS from Python using HTTP request

    Instead of using urllib, you may find life easier with the requests library. Oh, and remember to post code in [code][/code] bbcode tags, especially since indentation is significant in Python.

    You could end up with a simpler example:
    Code:
    ###########################################
    ## Ozeki NG - SMS Gateway Python example ##
    ###########################################
    
    import requests
    
    ############################
    ### Ozeki NG information ###
    ############################
    
    host = "http://127.0.0.1"
    
    user_name = "admin"
    user_password = "abc123"
    
    recipient = "+36304080332"
    
    message_body = "Hello World from Python"
    
    ############################
    #### Sending the message ###
    ############################
    
    response = requests.get(host + ":9501/api",
                            params={"action": "sendmessage",
                                    "username": user_name,
                                    "password": user_password,
                                    "recipient": recipient,
                                    "messagetype": "SMS:TEXT",
                                    "messagedata": message_body})
    
    ##############################
    ### Verifying the response ###
    ##############################
    
    if response.text.find("Message accepted for delivery") > 1:
        print("Message successfully sent")
    else:
        print("Message not sent! Please check your settings!")
    Last edited by laserlight; January 9th, 2013 at 11:39 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured