Python3应用程序带有'http.client.BadStatusLine'之后httpconnection.request即使异常处理程序存在

2024-09-28 13:08:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个问题我不明白。在

第一,我偶尔会得到http.client.BadStatusLine'错误httpconnection.request. 99.99%的时间代码正常工作,但偶尔会返回此错误。那么是我的代码,还是服务器造成了问题?在

第二,我在代码中有两个异常处理程序。第一个我把整个子例程放在周围,以捕捉我可能犯下的任何编码错误(应用程序运行无头,所以我想捕捉任何错误并以这种方式处理它们,这样我就可以记录错误并以整洁的方式终止应用程序)。第二个是关于实际请求的,所以,同样,我可以捕捉任何错误,报告它们,在这种情况下,让应用程序继续运行。奇怪的是,子例程异常处理程序捕获了http.client.BadStatusLine'而不是请求的异常处理程序。为什么会这样?在

我希望(和我下面的代码片段)都是有意义的,任何想法、建议或方法都可以确保我捕获http.client.BadStatusLine“作为一个可恢复的错误,而不是致命的错误,我们将非常感激地接受!”!!在

请原谅我的编码。我是老派,还没有真正掌握新的编码方法,所以我的大部分内容就像我在写一个旧的Fortran或VB程序!在

谢谢,吉姆

代码如下:

try:

    headers = {'X-Pvoutput-Apikey' : cArr[pvo_key][cVal],
               'X-Pvoutput-SystemId' : cArr[pvo_sysid][cVal],
               "Accept" : "text/plain",
               "Content-type": "application/x-www-form-urlencoded"}
    pvAddBaURL = "/service/r2/addbatchstatus.jsp"

    try:
        conn = http.client.HTTPConnection(pvo_host, timeout=ConTimeOut)
        #conn.set_debuglevel(2) # debug purposes only
        conn.request("POST", pvAddBaURL, "data=" + pvAddBaPrm, headers)
    except:
        errorReturn = sys.exc_info()
        writeLog(logWn, codeUpl, "Warning  > Batch upload connect unsuccessful")
        writeLog(logEr, codeUpl, "Bat > Batch upload unsuccessful with:")
        for errorVal in errorReturn:
            writeLog(logEr, codeUpl, "Bat > Connect error: " + str(errorVal))
        conn.close()
    else:
        response = conn.getresponse()
        if response.status != 200:
            writeLog(logWn, codeUpl, "Error   > Batch upload unsuccessful")
            writeLog(logEr, codeUpl, "Bat > Batch upload unsuccessful with:")
            writeLog(logEr, codeUpl, "Bat > Resp status: " + str(response.status))
            writeLog(logEr, codeUpl, "Bat > Resp reason: " + str(response.reason))
            writeLog(logEr, codeUpl, "Bat > Resp read: " + str(response.read()))
            conn.close()
        else:
            writeLog(logVb, codeUpl, "Bat > Upload successful")
            writeLog(logFu, codeUpl, "Bat > Resp status: " + str(response.status))
            writeLog(logFu, codeUpl, "Bat > Resp reason: " + str(response.reason))
            writeLog(logDb, codeUpl, "Bat > Resp read: " + str(response.read()))
            conn.close()

except:
    eespvo_Fatal(sys.exc_info(), codeUpl)  #fatal exception handler routine

#Routine to handle Crash!
#------------------------
#Ensures that even if there is a programming error the app can report it in the log.

def eespvo_Fatal(crashData, mod):

    writeLog(logWn, codeFaE, "Fatal    > eespvo (ProcId: "
         + str(eespvo_pid) +") terminated - restart attempted")
    writeLog(logFa, codeFaE, "Ftl > Fatal: eespvo crashed (ProcId: "
         + str(eespvo_pid) + ", Module: " + mod)

    for crashVal in crashData:
        writeLog(logFa, codeFaE, "Ftl > Fatal: " + str(crashVal))

    for frame in traceback.extract_tb(crashData[2]):
        fname, lineno, fn, text = frame
    writeLog(logFa, codeFaE, "Ftl > Fatal: <traceback line: " + str(lineno) + ">")
    writeLog(logFa, codeFaE, "Ftl > Fatal: <traceback module: " + str(fn) + ">")       
    writeLog(logFa, codeFaE, "Ftl > Fatal: <traceback text: " + str(text) + ">")

    writeLog(logFa, codeFaE, "Ftl > Fatal: eespvo (ProcId: " + str(eespvo_pid)
         +") killed")
    cArr[runOK][cVal] = False

#kill our own process
    os.kill(eespvo_pid,signal.SIGKILL)

日志的输出如下所示:

从子例程异常处理程序:

^{pr2}$

Tags: 代码response错误connrespstrbatftl
1条回答
网友
1楼 · 发布于 2024-09-28 13:08:02

我还是不知道问题1的答案。但我想我可能已经解决了问题2。我认为问题在于:

response = conn.getresponse()

这是失败的。在请求异常处理之外,它无法进行子例程异常处理。所以解决方案是把它放在请求异常处理中,vis:

^{pr2}$

只有时间会证明一切!在

相关问题 更多 >

    热门问题