Working with a 3G modem and SMS using Python

    Hi% username%!

    Recently, I had to face the following task: there is an old desktop with Windows XP installed on it. It is necessary that at a given time interval the program on this desktop send a GET request to the desired server, parse the response and, depending on the result, send SMS messages to the desired numbers. Of the additional devices, there was only a 3G-modem E160g from Billine, and connecting to the Internet via a wired network or WI-FI was physically impossible.

    The task was completed using only this modem and the Python language.

    Initially, it was decided to use a modem at the same time as an Internet connection device and an AT-modem for sending SMS. However, as one would expect, he could not work in two modes at once: when connected to the network, sending SMS became impossible.

    An attempt to connect two 3G modems (one for the Internet, the other for SMS) failed: the devices worked perfectly separately from each other, but with the simultaneous connection, the system saw only one.

    The replacement of one modem with a mobile phone had a positive effect, but it didn’t suit me: I didn’t have a desire to spend extra money on a phone, albeit an inexpensive one.

    As a result, I had the idea of ​​using one 3G modem, switching it to the necessary modes programmatically: first connect to the Internet through it, upload data, then disconnect, process the data, and send AT commands to send SMS. And all this is in Python.

    Firstly, there is no need for a second device, and secondly, connecting to the network will be rare and short-term, which will help save expensive 3G traffic.

    To send SMS, I used a wonderful script from the Habr topic. A simple script for sending SMS. But with the control of the Internet connection had to tinker. You can, of course, install the Python extension for win32 and manage it from the win-API functions. But it turned out that it was much easier to manipulate network connections using the usual RASDIAL utility.

    During installation of the program for the modem, a connection is created in the system through this modem. If it was not created or was deleted, then you can create it manually: “network connections” -> “Add”. We specify the modem in the properties, leave the user / password fields empty (because the user is identified by the SIM card number), enter the phone (in my case * 99 #), remove the “ask for username, password” checkboxes. Connection created.

    Now we program a small rasdial.py module that will allow us to manage this connection:
    1.  
    2. import os
    3.  
    4. def connect(connection):
    5.     command = r'RASDIAL %s' % connection
    6.     return os.system(command)
    7.  
    8. def disconnect(connection):
    9.     command = r'RASDIAL %s /DISCONNECT' % connection
    10.     return os.system(command)
    11.  

    Two functions of the module connect / disconnect the Internet for a single argument: the name of the connection. For example, if you called it “e160g”, then the call

    1.  
    2. import rasdial
    3.  
    4. code = rasdial.connect('e160g')
    5.  

    will try to connect and return the return status. If 0, then the connection is successful.

    As a result, the main program looked like this:

    1.  
    2. import sender
    3. import rasdial
    4. import httplib
    5.  
    6. code = rasdial.connect(conn_name)
    7. if code == 0:
    8.     conn = httplib.HTTPConnection(some_host)
    9.     conn.request('GET', query)
    10.     response = conn.getresponse()
    11.     rasdial.disconnect(conn_name)
    12.     # парсинг ответа
    13.     s = sender.Sender(port)
    14.     # отправка смс
    15.     s.send(*args)
    16.  

    I put the script on the crown with an interval of 1 hour. As a result, I managed with one modem and significantly saved traffic.
    Thanks for attention!

    Also popular now: