python:打印有关BadStatusLine错误的信息

时间:2014-10-30 15:04:17

标签: python httplib

我在执行以下python脚本后得到BadStatusLine。如何打印BadStatusLine的详细信息?

#!/usr/bin/python

import urllib
import urllib2
import httplib


try:
    # NoActiveDevsPerQtr
    request = urllib2.Request('http://127.0.0.1:8090')

    request.add_header('Accept',        'text/csv')
    request.add_header('User-Agent',    'python-script')

    request.add_data("""
        <? xml version="1.0"?>
        <log_query>
            <querytype>ListPerQtr</querytype>
            <year>2014</year>
            <quarter>3</quarter>
        </log_query>
    """)


    response = urllib2.urlopen(request)
    content  = response.read()
    print content

except httplib.BadStatusLine as e:
    print e

print e什么都不打印。

根据要求,删除除httplib.BadStatusLine以外的实际错误如下:

Traceback (most recent call last):
File "./IQueryTests.py", line 25, in <module>
response = urllib2.urlopen(request)
File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib64/python2.6/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib64/python2.6/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib64/python2.6/urllib2.py", line 1190, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib64/python2.6/urllib2.py", line 1163, in do_open
r = h.getresponse()
File "/usr/lib64/python2.6/httplib.py", line 990, in getresponse
response.begin()
File "/usr/lib64/python2.6/httplib.py", line 391, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python2.6/httplib.py", line 355, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine

1 个答案:

答案 0 :(得分:2)

来源(&#39; httplib.py&#39;)在一个案例中提到了这一点,它引发了BadStatusLine&#39; :

        if not line:
        # Presumably, the server closed the connection before
        # sending a valid response.
        raise BadStatusLine(line)

也许你正在遇到这种情况,暗示该行为空或空。

来源: https://hg.python.org/cpython/file/2.7/Lib/httplib.py

这看起来像一个疲惫的程序员决定;最好定义另一个例外,比如NoStatusLine&#39;