我是SMPP的新手,但我需要模拟SMPP协议上的流量。我已经找到了如何使用Python如何使用SMPP协议发送SMS中的smpp发送SMS的教程。
我正试着写一个接收器,但我无法让它开始工作。请帮帮忙。我的代码是:
import smpplib
class ClientCl():
client=None
def receive_SMS(self):
client=smpplib.client.Client('localhost',1000)
try:
client.connect()
client.bind_receiver("sysID","login","password")
sms=client.get_message()
print(sms)
except :
print("boom! nothing works")
pass
sms_getter=ClientCl.receive_SMS
发布于 2018-08-14 05:22:35
据我所知,您正在使用的smpplib是github中可用的。看看您的代码和客户端代码,我找不到函数client.get_message。也许你有一个旧版本的图书馆?否则我就找错图书馆了。在任何情况下,很可能get_message函数不会阻塞并等待消息到达。
从客户端代码来看,您似乎有两个选项:
如果您查看README.md文件,它将显示如何设置库以实现第二个选项(这是更好的选项)。
...
client = smpplib.client.Client('example.com', SOMEPORTNUMBER)
# Print when obtain message_id
client.set_message_sent_handler(
lambda pdu: sys.stdout.write('sent {} {}\n'.format(pdu.sequence, pdu.message_id)))
client.set_message_received_handler(
lambda pdu: sys.stdout.write('delivered {}\n'.format(pdu.receipted_message_id)))
client.connect()
client.bind_transceiver(system_id='login', password='secret')
for part in parts:
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()
...
当接收到消息或传递收据时,将调用client.set_message_received_handler()中定义的函数。在这个例子中,它是一个lambda函数。还有一个关于如何在线程中为侦听设置的示例。
如果您更喜欢更简单的轮询选项,则应该使用poll
函数。对于最简单的实现,您需要做的就是:
while True:
client.Poll()
与前面一样,client.set_message_received_handler()中的函数集将在消息到达后被调用。
https://stackoverflow.com/questions/50719825
复制相似问题