Skip to content

Commit 8f76c07

Browse files
committed
django example
1 parent 9a8baba commit 8f76c07

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

django_example/models.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# coding=utf8
2+
import logging
3+
4+
from django.db import models
5+
6+
# Create your models here.
7+
8+
SOURCE = ['tele2', 'beeline', 'mts', 'megafon']
9+
10+
ops = zip(SOURCE, SOURCE)
11+
import json
12+
13+
dirs = [[0, 'выход'], [1, 'вход']]
14+
import urllib.request
15+
from django.conf import settings
16+
17+
18+
class SMS(models.Model):
19+
class Meta:
20+
verbose_name = 'СМС'
21+
verbose_name_plural = 'СМС'
22+
23+
sphone = models.CharField(max_length=64, verbose_name='откуда', db_index=True)
24+
tphone = models.CharField(max_length=64, verbose_name='куда', db_index=True)
25+
message = models.TextField(verbose_name='текст сообщения')
26+
date = models.DateTimeField(auto_now_add=True, db_index=True)
27+
sended = models.BooleanField(default=False)
28+
op = models.CharField(choices=ops, max_length=16, verbose_name='оператор')
29+
direction = models.IntegerField(choices=dirs, default=0, verbose_name='направление')
30+
uuid = models.CharField(max_length=128, blank=True, null=True)

django_example/settings.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
SMPP = {
2+
'tele2': {
3+
'ip': '123.123.123.123',
4+
'port': 3331,
5+
'system_id': '123123',
6+
'password': '123123',
7+
'source': ['1234', 'asdasd'] #sender name
8+
},
9+
'mts': {
10+
'ip': 'smpp.mcommunicator.ru',
11+
'port': 123,
12+
'system_id': 'asdasd',
13+
'password': 'asdasd',
14+
'source': ['123123', 'ASDAS']
15+
},
16+
'beeline': {
17+
'ip': '123.123.123.123',
18+
'port': 3340,
19+
'system_id': '12321321',
20+
'password': 'asdasda',
21+
'source': ['1234']
22+
},
23+
'megafon': {
24+
'ip': '10.236.123.123',
25+
'port': 12000,
26+
'system_id': 'MSK_asdasd',
27+
'password': 'asdsadas',
28+
'source': ['1234']
29+
},
30+
31+
}
32+

django_example/smpp-serve.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# coding=utf8
2+
from django.core.management.base import BaseCommand
3+
4+
import datetime
5+
import logging
6+
import smpplib.gsm
7+
import smpplib.client
8+
import smpplib.consts
9+
10+
h1 = datetime.timedelta(hours=1)
11+
12+
from django.conf import settings
13+
14+
def mkGetPdu(op: str):
15+
def getPdu(pdu):
16+
try:
17+
source = pdu.parsed['saddress']
18+
target = pdu.parsed['daddress']
19+
message = pdu.parsed.get('sm', '') + pdu.parsed.get('payload', '')
20+
from sms import models
21+
if 'submit date:' not in message:
22+
sms = models.SMS(direction=1, sphone=source, tphone=target, message=message, sended=True, op=op)
23+
sms.message = "".join(filter(lambda x: x not in ['\u0000'], sms.message))
24+
sms.save()
25+
26+
except Exception as e:
27+
logging.error(e)
28+
29+
return getPdu
30+
31+
32+
def mkGenMessages(op, num):
33+
def GenMessages():
34+
from sms.models import SMS
35+
newSMS = list(SMS.objects.filter(op=op, direction=0, sended=False)[:100])
36+
37+
if newSMS:
38+
for sms in newSMS:
39+
if not sms.sphone:
40+
sms.sphone = num
41+
sms.sended = True
42+
sms.save()
43+
yield (sms.sphone, sms.tphone, sms.message)
44+
yield None
45+
46+
return GenMessages
47+
48+
49+
class Command(BaseCommand):
50+
help = 'serve SMPP requests'
51+
52+
def add_arguments(self, parser):
53+
parser.add_argument('op', help='tele2/mts/beeline')
54+
55+
def handle(self, *args, **options):
56+
logging.basicConfig(level='DEBUG')
57+
try:
58+
op = options['op']
59+
s = settings.SMPP[op]
60+
except:
61+
logging.error("no such config")
62+
exit()
63+
64+
timeout = 15
65+
if options['op'] == 'megafon':
66+
timeout = 60
67+
client = smpplib.client.Client(s['ip'], s['port'], timeout)
68+
69+
client.set_message_received_handler(mkGetPdu(op))
70+
client.set_message_source(mkGenMessages(op, s['source']))
71+
client.connect()
72+
73+
client.bind_transceiver(system_id=s['system_id'], password=s['password'])
74+
try:
75+
client.listen([11])
76+
except Exception as e:
77+
logging.error(e)

pdu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def parse(self):
192192
self._parseCMD('s msgid')
193193
if self._pdu['cmdid'] in [b'00000004', b'00000005']:
194194
self._parseCMD("s servtype 2 saddrton 2 saddrnpi s saddress 2 daddrton 2 daddrnpi s daddress " +
195-
"2 esm 2 pid 2 priority s sdt s valt 2 rdel 2 rip 2 dcs 2 smid 2 smlen ls sm "+
195+
"2 esm 2 pid 2 priority s sdt s valt 2 rdel 2 rip 2 dcs 2 smid 2 smlen ls sm " +
196196
"4 tag 4 paylen pl payload")
197197
if self._pdu['cmdid'] in [b'00000103']:
198198
self._parseCMD('s servtype 2 saddrton 2 saddrnpi s saddress 2 daddrton 2 daddrnpi s daddress ' +

0 commit comments

Comments
 (0)