Пример работы с SMS шлюзом на языке Python

Ниже представлены пример подключения к СМС шлюзу на Python

Отправка СМС:

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
login = ""
password = ""
  
phone_sms = "380633333131"
msg_id = "123456"
 
send_sms = '''<?xml version="1.0" encoding="UTF-8"?>
<SMS>
<operations>
<operation>SEND</operation>
</operations>
<authentification>
<username>%s</username>
<password>%s</password>
</authentification>
<message>
<sender>SMS</sender>
<text>Test message [UTF-8]</text>
</message>
<numbers>
<number messageID="%s">%s</number>
</numbers>
</SMS>''' % (login, password, msg_id, phone_sms)
 
import urllib2, urllib
senddata=[('XML',send_sms)]
senddata=urllib.urlencode(senddata)
req=urllib2.Request(path, senddata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
result=urllib2.urlopen(req).read()
print result

Получения статуса отправленной СМС (информация о статусе смс будет доступна спустя несколько минут после отправки):

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
login = ""
password = ""
  
phone_sms = "380633333131"
msg_id = "123456"
 
get_sms_status = '''<?xml version="1.0" encoding="UTF-8"?>
<SMS>
<operations>
<operation>GETSTATUS</operation>
</operations>
<authentification>
<username>%s</username>
<password>%s</password>
</authentification>
<message>
<sender>SMS</sender>
<text>Test message [UTF-8]</text>
</message>
<numbers>
<number messageID="%s">%s</number>
</numbers>
</SMS>''' % (login, password, msg_id, phone_sms)
 
import urllib2, urllib
senddata=[('XML',get_sms_status)]
senddata=urllib.urlencode(senddata)
req=urllib2.Request(path, senddata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
result=urllib2.urlopen(req).read()
print result

Получение цены отправки:

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
login = ""
password = ""
  
phone_sms = "380633333131"
msg_id = "123456"
 
get_send_price = '''<?xml version="1.0" encoding="UTF-8"?>
<SMS>
<operations>
<operation>GETPRICE</operation>
</operations>
<authentification>
<username>%s</username>
<password>%s</password>
</authentification>
<message>
<sender>SMS</sender>
<text>Test message [UTF-8]</text>
</message>
<numbers>
<number messageID="%s">%s</number>
</numbers>
</SMS>''' % (login, password, msg_id, phone_sms)
 
import urllib2, urllib
senddata=[('XML',get_sms_status)]
senddata=urllib.urlencode(senddata)
req=urllib2.Request(path, senddata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
result=urllib2.urlopen(req).read()
print result

Получение баланса:

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
login = ""
password = ""
  
phone_sms = "380633333131"
msg_id = "123456"
 
get_balance = '''<?xml version="1.0" encoding="UTF-8"?>
<SMS>
<operations>
<operation>BALANCE</operation>
</operations>
<authentification>
<username>%s</username>
<password>%s</password>
</authentification>
</SMS>''' % (login, password)
 
import urllib2, urllib
senddata=[('XML',get_balance)]
senddata=urllib.urlencode(senddata)
req=urllib2.Request(path, senddata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
result=urllib2.urlopen(req).read()
print result

vector
image

Нужна помощь?

Задайте вопрос специалисту технической поддержки