urllib异常http.client.BadStatusLine [英] urllib exception http.client.BadStatusLine

查看:4161
本文介绍了urllib异常http.client.BadStatusLine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生活找出为什么我不能捕捉到这个例外。



在这里查看本指南

  def get_team_names(get_team_id_url,team_id):
print(get_team_id_url + team_id)
尝试:
response = urllib.request.urlopen(get_team_id_url + team_id)
除了urllib.error.HTTPError作为e:
print(e.code)
print(e。 read())
除了urllib.error.URLError作为e:
print(e.code)
print(e.read())

例外:

 追溯(最近的呼叫):
文件queue_cleaner_main.py,第60行,在< module>
sys.exit(main())
文件queue_cleaner_main.py,第57行,主要是
team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL,team [2])
文件D:\oppssup\old_job\queue_cleaner_functions.py,第132行,get_team_names
response = urllib.request.urlopen(get_team_id_url + team_id)
文件C:\Python34\ lib \urllib\request.py,第153行,urlopen
return opener.open(url,data,timeout)
文件C:\Python34\lib\urllib\ request.py,第455行,打开
response = self._open(req,data)
文件C:\Python34\lib\urllib\request.py,第473行,在_open
'_open',req)
文件C:\Python34\lib\urllib\request.py,第433行,_call_chain
result = func * args)
文件C:\Python34\lib\urllib\request.py,第1202行,在http_open
中返回self.do_open(http.clien t.HTTPConnection,req)
文件C:\Python34\lib\urllib\request.py,第1177行,do_open
r = h.getresponse()
文件C:\Python34\lib\http\client.py,第1172行,getresponse
response.begin()
文件C:\Python34\lib\http \client.py,行351,开始
版本,status,reason = self._read_status()
文件C:\Python34\lib\http\client.py ,line 321,in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine:''

我的整个方法似乎非常cluncky。我可以找出一个优雅的方式来处理异常并处理没有结果。

  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)
除了异常作为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'])
在r_json ['result']中:
print(我['group'])

异常:

 追溯(最近的最后一次呼叫):
文件queue_cleaner_main.py,第60行,< module>
sys.exit(main())
文件queue_cleaner_main.py,第57行,主要是
team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL,team [2])
文件D:\oppssup\old_job\queue_cleaner_functions.py,第141行,get_team_names
r_json = json.loads(response.read()。decode('utf8')
UnboundLocalError:在分配前引用的局部变量响应

任何反馈将不胜感激。

这个 http.client.BadStatusLine http.client.HTTPException 。我想如果你这样做:

  try:
response = urllib.request.urlopen(get_team_id_url + team_id)
除了http.client.HTTPException为e:
print(e)

那么你捕捉它不应该有问题。但是,究竟是什么造成的,也许是你应该关心的。


I can't for the life of me figure out why I can't catch this exception.

Looking here at 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: ''

also my entire method seems to be very "cluncky". I can figure out an elegant way to handle the exception and deal with no result.

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

Any feedback would be greatly appreciated.

解决方案

This http.client.BadStatusLine is a subclass of http.client.HTTPException. I think if you do:

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

then you shouldn't have problem catching it. However, what caused it is perhaps what you should concern.

这篇关于urllib异常http.client.BadStatusLine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆