通过Python发送短信

时间:2012-04-07 09:39:44

标签: python www-authenticate

我正在使用以下代码连接手机以发送短信:

import serial
import time

phone = serial.Serial()
phone.baudrate = 38400
phone.bytesize = 8
phone.stopbits = 1
phone.xonxoff = 0
phone.rtscts = 0
phone.timeout = 0
phone.port = 3  #try different ports here, if this doesn't work.
phone.parity=serial.PARITY_NONE
phone.open()
print phone.portstr


recipient = "+929409778"
message = "We did it!"
print ("I did come after it")
try:
    time.sleep(0.5)
    phone.write(b'ATZ\r')
    time.sleep(0.5)




    phone.write(b'AT+CMGF=1\r')
    time.sleep(0.5)
    phone.write(b'AT+CMGS="' + recipient.encode() + b'"\r')
    time.sleep(0.5)
    phone.write(message.encode() + b"\r")

    print (message)
    time.sleep(0.5)
    phone.write(bytes([26]))

    time.sleep(0.5)
    phone.readall()


finally:
    phone.close()

我可以连接手机,但我无法发送信息。它没有给我一个错误,所以我不知道从哪里开始调试。

我的代码是否有问题,我没有看到?

1 个答案:

答案 0 :(得分:0)

尝试使用phone.flush()刷新手机信息流,看看是否通过它发送。