urllib异常http.client.BadStatusLine

时间:2015-05-01 03:16:50

标签: exception python-3.x urllib

我不能为我的生活找出为什么我无法捕获这个例外。

this guide

def get_team_names(get_team_id_url, team_id):
    print(get_team_id_url + team_id)
    try:
        response = urllib.request.urlopen(get_team_id_url + team_id)
    except urllib.error.HTTPError as e:
        print(e.code)
        print(e.read()) 
    except urllib.error.URLError as e:
        print(e.code)
        print(e.read()) 

异常:

Traceback (most recent call last):
  File "queue_cleaner_main.py", line 60, in <module>
    sys.exit(main())
  File "queue_cleaner_main.py", line 57, in main
    team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL, team[2])
  File "D:\oppssup\old_job\queue_cleaner_functions.py", line 132, in get_team_names
    response = urllib.request.urlopen(get_team_id_url + team_id)
  File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 455, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 473, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1202, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1177, in do_open
    r = h.getresponse()
  File "C:\Python34\lib\http\client.py", line 1172, in getresponse
    response.begin()
  File "C:\Python34\lib\http\client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "C:\Python34\lib\http\client.py", line 321, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: ''

另外,我的整个方法似乎非常笨重。我无法找到一种处理异常的优雅方法,包括没有返回结果的情况。

def get_team_names(get_team_id_url, team_id):
    print(get_team_id_url + team_id)
    try:
        response = urllib.request.urlopen(get_team_id_url + team_id)
    except Exception as e:
        #print(e.code)
        #print(e.read())
        print('shit')
    #print(response.read().decode('utf8'))
    r_json = json.loads(response.read().decode('utf8'))
    print(r_json['result'])
    for i in r_json['result']:
        print(i['group'])

例外:

Traceback (most recent call last):
  File "queue_cleaner_main.py", line 60, in <module>
    sys.exit(main())
  File "queue_cleaner_main.py", line 57, in main
    team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL, team[2])
  File "D:\oppssup\old_job\queue_cleaner_functions.py", line 141, in get_team_names
    r_json = json.loads(response.read().decode('utf8'))
UnboundLocalError: local variable 'response' referenced before assignment

1 个答案:

答案 0 :(得分:4)

http.client.BadStatusLinehttp.client.HTTPException的子类。我想如果你这样做:

try:
    response = urllib.request.urlopen(get_team_id_url + team_id)
except http.client.HTTPException as e:
    print(e)
那么你应该没有问题抓住它。然而,导致它的原因可能是你应该关注的。