用于python的smpp库
smpplib的Python项目详细描述
python-smpplib
用于python的smpp库。从Google Code分叉。
示例:
importloggingimportsysimportsmpplib.gsmimportsmpplib.clientimportsmpplib.consts# if you want to know what's happeninglogging.basicConfig(level='DEBUG')# Two parts, UCS2, SMS with UDHparts,encoding_flag,msg_type_flag=smpplib.gsm.make_parts(u'Привет мир!\n'*10)client=smpplib.client.Client('example.com',SOMEPORTNUMBER)# Print when obtain message_idclient.set_message_sent_handler(lambdapdu:sys.stdout.write('sent {}{}\n'.format(pdu.sequence,pdu.message_id)))client.set_message_received_handler(lambdapdu:sys.stdout.write('delivered {}\n'.format(pdu.receipted_message_id)))client.connect()client.bind_transceiver(system_id='login',password='secret')forpartinparts:pdu=client.send_message(source_addr_ton=smpplib.consts.SMPP_TON_INTL,#source_addr_npi=smpplib.consts.SMPP_NPI_ISDN,# Make sure it is a byte string, not unicode:source_addr='SENDERPHONENUM',dest_addr_ton=smpplib.consts.SMPP_TON_INTL,#dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN,# Make sure thease two params are byte strings, not unicode:destination_addr='PHONENUMBER',short_message=part,data_coding=encoding_flag,esm_class=msg_type_flag,registered_delivery=True,)print(pdu.sequence)client.listen()
您可能还想在一个线程中收听:
fromthreadingimportThreadt=Thread(target=client.listen)t.start()
客户机支持设置一个自定义生成器,该生成器为pdu包生成序列号。默认情况下,使用一个简单的内存生成器,它在客户端的(重新)实例化时重置,例如通过应用程序重新启动。如果希望在重新启动时保留序列号,可以实现自己的存储支持生成器。
示例:
importsmpplib.clientimportmymodulegenerator=mymodule.PersistentSequenceGenerator()client=smpplib.client.Client('example.com',SOMEPORTNUMBER,sequence_generator=generator)...