python-gsmmodem短信發(fā)送和接收庫
Python實現(xiàn)的短信發(fā)送/接收庫,支持多種型號的短信貓。主要功能有:
發(fā)送短信、檢測信號強度
基于回調(diào)的來電和短信處理方法
支持短信狀態(tài)報告追蹤
使用Python異常包裹AT命令錯誤
模塊化代碼,具有良好的擴展能力
提供了一些簡單的調(diào)試工具。
示例
短信接收和回復(fù)
#!/usr/bin/env python
"""\
Demo: handle incoming SMS messages by replying to them
Simple demo app that listens for incoming SMS messages, displays the sender's number
and the messages, then replies to the SMS by saying "thank you"
"""
from __future__ import print_function
import logging
PORT = '/dev/ttyUSB2'
BAUDRATE = 115200
PIN = None # SIM card PIN (if any)
from gsmmodem.modem import GsmModem
def handleSms(sms):
print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text))
print('Replying to SMS...')
sms.reply(u'SMS received: "{0}{1}"'.format(sms.text[:20], '...' if len(sms.text) > 20 else ''))
print('SMS sent.\n')
def main():
print('Initializing modem...')
# Uncomment the following line to see what the modem is doing:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
modem = GsmModem(PORT, BAUDRATE, smsReceivedCallbackFunc=handleSms)
modem.smsTextMode = False
modem.connect(PIN)
print('Waiting for SMS message...')
try:
modem.rxThread.join(2**31) # Specify a (huge) timeout so that it essentially blocks indefinitely, but still receives CTRL+C interrupt signal
finally:
modem.close();
if __name__ == '__main__':
main()評論
圖片
表情
