如何测试stdin脚本" live",通常程序在后台与之交互?

时间:2014-05-13 02:14:12

标签: python stdout stdin

我的脚本如下:

import os
import sys
import time
import MySQLdb

if __name__=="__main__":

dbcon = MySQLdb.connect(host="host", port=3306, user="user", passwd="passwd", db="db")
dbcur = dbcon.cursor()

deliveryCount = 0
bounceBadMailbox = 0
bounceInactiveAccount = 0
bouncePolicyRelated = 0
bounceSpamRelated = 0
bounceQuotaIssues =0

while True:
    #type, timeLogged,timeQueued,orig,rcpt,orcpt,dsnAction,dsnStatus,dsnDiag,dsnMta,bounceCat,srcType,srcMta,dlvType,dlvSourceIp,dlvDestinationIp,dlvEsmtpAvailable,dlvSize,vmta,jobId,envId,queue,vmtaPool
    line = sys.stdin.readline()
    fwrite = open("debug.log","w")
    fwrite.write("in true loop")

    logList = line.split(',')

    bounceType = logList[0]
    bounceCategory = logList[10]
    emailAddress = logList[4]
    jobId = logList[19]

    if bounceType == 'd':
        deliveryCount += 1
        dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %d WHERE id = %d', (deliveryCount,jobId))
        dbcon.commit()

    elif bounceType == 'b':
        if bounceCategory == 'bad-mailbox':
            bounceBadMailbox +=1
            dbcur.execute('UPDATE list_users SET status = %s, modified_date=NOW() WHERE email=%s', ('b',emailAddress))
            dbcur.execute('UPDATE campaign_stat_delivered SET bad_mailbox = bad_mailbox + %d WHERE id = %d', (bounceBadMailbox,jobId))
            dbcon.commit()

        elif bounceCategory =='inactive-account':
            bounceInactiveAccount +=1
            dbcur.execute('UPDATE list_users SET status = %s, modified_date=NOW() WHERE email=%s', ('i',emailAddress))
            dbcur.execute('UPDATE campaign_stat_delivered SET inactive_account = inactive_account + %d WHERE id = %d', (bounceInactiveAccount,jobId))
            dbcon.commit()

        elif bounceCategory =='policy-related':
            bounceInactiveAccount +=1
            dbcur.execute('UPDATE list_users SET status = %s, modified_date=NOW() WHERE email=%s', ('p',emailAddress))
            dbcur.execute('UPDATE campaign_stat_delivered SET policy_related = policy_related + %d WHERE id = %d', (bouncePolicyRelated,jobId))
            dbcon.commit()

        elif bounceCategory =='spam-related':
            bounceInactiveAccount +=1
            dbcur.execute('UPDATE list_users SET status = %s, modified_date=NOW() WHERE email=%s', ('s',emailAddress))
            dbcur.execute('UPDATE campaign_stat_delivered SET spam_related = spam_related + %d WHERE id = %d', (bounceSpamRelated,jobId))
            dbcon.commit()

        elif bounceCategory =='quota-issues':
            bounceInactiveAccount +=1
            dbcur.execute('UPDATE list_users SET status = %s, modified_date=NOW() WHERE email=%s', ('q',emailAddress))
            dbcur.execute('UPDATE campaign_stat_delivered SET quota_issues = quota_issues + %d WHERE id = %d', (bounceQuotaIssues,jobId))
            dbcon.commit()


fwrite = open("debug2.log","w")
fwrite.write("out of true loop")

dbcon.close()

我没有得到任何结果,并且它花了我很长时间来调试,因为我必须修改脚本,重新启动我的程序以提供给stdin,然后尝试我的行代码。

我有办法吗?

python mystdinscript.py

并查看实际结果? ...即。我需要做些什么来创建一个可以提供给正在运行的stdin脚本的python脚本?

就目前而言,我的程序会自动编入此脚本,而且我看不到任何结果 - 因此调试起来非常困难。

1 个答案:

答案 0 :(得分:0)

查看输出的最快方法是将fwrite设置为stdout:

#fwrite = open(...)
fwrite = sys.stdout

这会将结果打印到您的屏幕而不是文件。