想写一个脚本实时监控秒杀列表中指定商品的库存,并发邮件提醒
目前有两个问题:
1. 程序使用最下方注释掉的代码,这样每个商品都要重复写几行语句,能正常运行,但是在有多个商品符合条件时发送邮件后提示“ connection unexpectedly closed ”,好像是 smtp 的问题?
2. 把那几条语句封装一下,即现在用的 check 函数,但是后台只看到 goodName1 在运行, goodName2 根本没执行?
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
import requests, re, json, threading
from threading import Timer
import time
import sys, os
reload(sys)
sys.setdefaultencoding('utf8')
def send_mail(to_list, sub, content):
...
class Buy(object):
stockNum = 0
totalStockNum = 0
def __init__(self, name):
self.goodsName = name
def check_stock(self):
r1 = requests.get()
listNum = len(r1.json()["data"]["list"])
for i in range(1, listNum, 1):
goodName = r1.json()["data"]["list"][i - 1]["goodsName"]
if self.goodsName in goodName:
stock = r1.json()["data"]["list"][i - 1]["stockNum"] #获取当前可售库存
total = r1.json()["data"]["list"][i - 1]["totalStockNum"] #获取当前总库存
add = total - self.totalStockNum #计算本次新增库存
if stock <> 0 and stock > self.stockNum:
send_mail(mailto_list, goodName + str(stock), str(add))
print "send success"
self.stockNum = stock
self.totalStockNum = total
def check(goodName):
p1 = Buy(goodName)
while True:
timer1 = threading.Timer(5, Buy.check_stock, (p1,))
timer1.start()
timer1.join()
if __name__ == '__main__':
check("goodName1")
check("goodName2")
# p1 = Buy("goodName1")
# p2 = Buy("goodName2")
# while True:
# timer1 = threading.Timer(5, Buy.check_stock, (p1,))
# timer2 = threading.Timer(5, Buy.check_stock, (p2,))
# timer1.start()
# timer2.start()
目前有两个问题:
1. 程序使用最下方注释掉的代码,这样每个商品都要重复写几行语句,能正常运行,但是在有多个商品符合条件时发送邮件后提示“ connection unexpectedly closed ”,好像是 smtp 的问题?
2. 把那几条语句封装一下,即现在用的 check 函数,但是后台只看到 goodName1 在运行, goodName2 根本没执行?
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
import requests, re, json, threading
from threading import Timer
import time
import sys, os
reload(sys)
sys.setdefaultencoding('utf8')
def send_mail(to_list, sub, content):
...
class Buy(object):
stockNum = 0
totalStockNum = 0
def __init__(self, name):
self.goodsName = name
def check_stock(self):
r1 = requests.get()
listNum = len(r1.json()["data"]["list"])
for i in range(1, listNum, 1):
goodName = r1.json()["data"]["list"][i - 1]["goodsName"]
if self.goodsName in goodName:
stock = r1.json()["data"]["list"][i - 1]["stockNum"] #获取当前可售库存
total = r1.json()["data"]["list"][i - 1]["totalStockNum"] #获取当前总库存
add = total - self.totalStockNum #计算本次新增库存
if stock <> 0 and stock > self.stockNum:
send_mail(mailto_list, goodName + str(stock), str(add))
print "send success"
self.stockNum = stock
self.totalStockNum = total
def check(goodName):
p1 = Buy(goodName)
while True:
timer1 = threading.Timer(5, Buy.check_stock, (p1,))
timer1.start()
timer1.join()
if __name__ == '__main__':
check("goodName1")
check("goodName2")
# p1 = Buy("goodName1")
# p2 = Buy("goodName2")
# while True:
# timer1 = threading.Timer(5, Buy.check_stock, (p1,))
# timer2 = threading.Timer(5, Buy.check_stock, (p2,))
# timer1.start()
# timer2.start()