我是python的初学者,我正在寻找一个通过华为调制解调器发送和接收短信的图书馆。我尝试了gammu,pysms和pygsm但未能让他们工作。你能给我这些库的代码示例吗?
答案 0 :(得分:1)
您可以尝试使用此代码,它适用于我,只需插入USB加密狗并获取其设备节点路径(在linux中使用lsusb和ls -lha / dev / tty *)并将/dev/ttyACM0
替换为该路径然后你应该发送短信,这段代码适用于华为USB调制解调器。
#!/usr/bin/env python
"""
sms.py - Used to send txt messages.
"""
import serial
import time
class TextMessage:
def __init__(self, recipient="0123456789", message="TextMessage.content not set."):
self.recipient = recipient
self.content = message
def setRecipient(self, number):
self.recipient = number
def setContent(self, message):
self.content = message
def connectPhone(self):
self.ser = serial.Serial('/dev/ttyACM0', 460800, timeout=5)
time.sleep(1)
def sendMessage(self):
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')
time.sleep(1)
self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
time.sleep(1)
self.ser.write(self.content + "\r")
time.sleep(1)
self.ser.write(chr(26))
time.sleep(1)
def disconnectPhone(self):
self.ser.close()
有关详细信息,请尝试此link(archive of that dead link as of 2014-08-25)