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 sent a GET request to the correct server, parsed the response and, depending on the result, sent SMS messages to the necessary numbers. Of the additional devices, there was only a 3G-modem E160g from Billine, and the Internet connection via a wired network or WI-FI was physically impossible.
The task was performed using only this modem and the Python language.
Initially, it was decided to use the modem at the same time as an Internet connection device and an AT modem for sending SMS. However, as one would expect, it could not work in two modes at once: when connected to the network, sending SMS became impossible.
')
Attempting to connect two 3G modems (one for the Internet, the other for SMS) failed: the devices functioned perfectly separately from each other, but when connected simultaneously, the system saw only one.
The positive effect was given by replacing one modem on a mobile phone, but this did not suit me: I did not have the desire to spend extra money on a phone, even if it was inexpensive.
As a result, I had the idea to use one 3G modem, switching it to the required modes programmatically: first connect to the Internet through it, upload data, then disconnect, process data, and send AT commands to send SMS. And all this in Python.
Firstly, there is no need for a second device, and secondly, connecting to the network will be rare and short-lived, which will help save expensive 3G traffic.
For sending SMS, I used a wonderful script from a habr-topic.
A simple script for sending SMS . But with the management of Internet connection had to tinker. You can, of course, put the Python extension for win32 and manage it from the win-API functions. But it turned out that it is much easier to manipulate network connections using the usual RASDIAL utility.
During the installation of the program for the modem, the system creates a connection through this modem. If it was not created or was deleted, you can create it manually: “network connections” -> “Add”. 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 checkboxes “ask for the username, password”. Connection created.
Now let's program a small module rasdial.py, which will allow us to manage this connection:
import os def connect(connection): command = r 'RASDIAL %s' % connection return os .system(command) def disconnect(connection): command = r 'RASDIAL %s /DISCONNECT' % connection return os .system(command)
import os def connect(connection): command = r 'RASDIAL %s' % connection return os .system(command) def disconnect(connection): command = r 'RASDIAL %s /DISCONNECT' % connection return os .system(command)
import os def connect(connection): command = r 'RASDIAL %s' % connection return os .system(command) def disconnect(connection): command = r 'RASDIAL %s /DISCONNECT' % connection return os .system(command)
import os def connect(connection): command = r 'RASDIAL %s' % connection return os .system(command) def disconnect(connection): command = r 'RASDIAL %s /DISCONNECT' % connection return os .system(command)
import os def connect(connection): command = r 'RASDIAL %s' % connection return os .system(command) def disconnect(connection): command = r 'RASDIAL %s /DISCONNECT' % connection return os .system(command)
import os def connect(connection): command = r 'RASDIAL %s' % connection return os .system(command) def disconnect(connection): command = r 'RASDIAL %s /DISCONNECT' % connection return os .system(command)
import os def connect(connection): command = r 'RASDIAL %s' % connection return os .system(command) def disconnect(connection): command = r 'RASDIAL %s /DISCONNECT' % connection return os .system(command)
Two functions of the module make the connection / disconnection of the Internet by a single argument: the name of the connection. For example, if you called it "e160g", then the call
- import rasdial
- code = rasdial.connect ( 'e160g' )
will attempt to connect and return the status of the return. If 0, then the connection was successful.
As a result, the main program looked like this:
- import sender
- import rasdial
- import httplib
- code = rasdial.connect (conn_name)
- if code == 0 :
- conn = httplib .HTTPConnection (some_host)
- conn.request ( 'GET' , query)
- response = conn.getresponse ()
- rasdial.disconnect (conn_name)
- # parsing response
- s = sender.Sender (port)
- # send SMS
- s.send ( * args)
I ran the script on crowns with an interval of 1 hour. As a result, I managed with one modem and saved a lot of traffic.
Thanks for attention!