exception - urllib 异常 http.client.BadStatusLine

标签 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()) 

异常(exception):

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'])

异常(exception):

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

最佳答案

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

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

那么你捕捉它应该没有问题。然而,是什么原因造成的,也许才是你应该关心的。

关于exception - urllib 异常 http.client.BadStatusLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29981182/

相关文章:

python - 将数据保存在三个数组中

python - 编写 MongoDB 查询来根据引用的属性进行过滤?

Python 使用 urllib 对网页进行身份验证

Python urllib 无法打开 localhost

python - 匹配网页内容中的文本但抛出索引错误

javascript - 全局错误过滤器不捕获来自服务的异常,只捕获 Controller

c++ - 如何使用 C++ 驱动程序重新连接到 MongoDB?

python-3.x - 在 conda 环境中安装 utils(不是 python-utils)

调用 List.remove(index) 时出现 java.lang.UnsupportedOperationException

java - 在运行时抛出异常