【问题标题】:urllib exception http.client.BadStatusLineurllib 异常 http.client.BadStatusLine
【发布时间】:2015-05-01 03:16:50
【问题描述】:

我一辈子都想不通为什么我不能捕捉到这个异常。

看这里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

【问题讨论】:

    标签: exception python-3.x urllib


    【解决方案1】:

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

    try:
        response = urllib.request.urlopen(get_team_id_url + team_id)
    except http.client.HTTPException as e:
        print(e)
    

    那么你应该没有问题抓住它。但是,导致它的原因可能是您应该关注的。

    【讨论】:

    • 嗨,天际线感谢您的回复。当我添加该行时,我得到以下错误文件“D:\oppssup\old_job\queue_cleaner_functions.py”,第 112 行,在 get_team_name 中,除了 http.client.HTTPException 为 e: NameError: name 'http' is not defined
    • 你导入http了吗?
    • 感谢修复它的伙伴。为什么我什至不使用它时需要导入http,只是为了捕获异常?我认为异常处理仅由使用的每个导入模块处理,不必导入特定模块只是为了处理另一个模块中的异常?
    • 我认为这可能是python3中urllib的某种设计缺陷。我也不喜欢导入其他库。
    • 3 年多后帮助了我。谢了哥们! py3.6 在这里。
    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2016-12-27
    • 2015-08-26
    • 2016-12-20
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多