我在日期比较方面遇到了一些问题。功能检查域和时间的DB,如果找不到域,则执行代码并插入到DB中。如果存在域和日期,则检查日期是否早于24小时并执行功能。
数据库条目如下所示:
domain inform_date
pl23.domain.lt 1426690221
功能是:
### Inform function
def information( domain ):
inform_timelimit = now - (inform_time*3600)
cur.execute("SELECT inform_date FROM php_inform_date WHERE `domain` = %s", (domain))
time_send = cur.fetchone()
if time_send == None:
cur.execute("REPLACE INTO php_inform_date VALUES (%s, %s)", (domain, now))
(...)
if inform_timelimit > time_send[0]:
cur.execute("REPLACE INTO php_inform_date VALUES (%s, %s)", (domain, now))
(...)
变量timelimit是unixtime:
now = int(time.time())
inform_timelimit = now - (24*3600)
找到域时出错:
Traceback (most recent call last):
File "/opt/php-secure-sendmail/secure_sendmail.py", line 84, in <module>
information(domain)
File "/opt/php-secure-sendmail/secure_sendmail.py", line 41, in information
if inform_timelimit > time_send[0]:
TypeError: 'NoneType' object is unsubscriptable
PS。抱歉我的英语不好;)
答案 0 :(得分:0)
我找到了解决方案。只需改变逻辑:
### Inform function
def information( domain ):
cur.execute("SELECT inform_date FROM php_inform_date WHERE `domain` = %s", (domain))
time_send = cur.fetchone()
### if no information about time in DB do action:
if time_send == None:
(.....)
else:
### if found entry, then check 24 hour limit / inform_time - includet from config file (24 hours / or can change)
inform_timelimit = now - time_send[0]
if inform_timelimit > inform_time:
(.....)