当前位置:   article > 正文

python 向usb发送指令,Python通过USB串行发送许多短信

usb.codesend

I'm really lost now at this part where I need to send sms to multiple recipients.

How do i send an sms to all the contacts i have in my database? Do I use for-loops? Or is there other ways? Please help thank you so much.

This is my SMS code

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import serial

import time

import sys

import MySQLdb as mdb

try:

con = mdb.connect('localhost', 'user', 'password', 'db')

print 'Database connected';

except Exception as e:

sys.exit('Database connection failed')

cur = con.cursor()

cur.execute("Select contacts from dbtable")

con.commit()

number= cur.fetchall()

print number

for item in number:

recipient= recipient+item

class TextMessage:

def __init__(self, recipient="xxxxxxxx",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/ttyUSBSMS', 460800, timeout=5, xonxoff = False, rtscts = False, bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE)

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()

sms = TextMessage("xxxxxxxx","Important!")

sms.connectPhone()

sms.sendMessage()

sms.disconnectPhone()

print "message sent successfully"

The printed output

(('99876545',), ('87546412',), ('97789546',), ('87546464',), ('97377454',))

解决方案

A simple for loop would do the job:

t_list = (('99876545',), ('87546412',), ('97789546',), ('87546464',), ('97377454',))

for t in t_list:

number = t[0]

print(number)

# Call function to send SMS to the given `number`

Just adapt this basic approach do fulfill your needs.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/555027
推荐阅读
相关标签
  

闽ICP备14008679号