为什么我在 python 中收到这个错误?(httplib) [英] Why am I getting this error in python ? (httplib)

查看:42
本文介绍了为什么我在 python 中收到这个错误?(httplib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if theurl.startswith("http://"): theurl = theurl[7:]head = theurl[:theurl.find('/')]tail = theurl[theurl.find('/'):]response_code = 0导入 httplibconn = httplib.HTTPConnection(head)conn.request("HEAD",tail)res = conn.getresponse()response_code = int(res.status)http://www.garageband.com/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3回溯(最近一次调用最后一次):文件check_data_404.py",第 51 行,在 <module> 中跑()运行中的文件check_data_404.py",第 35 行res = conn.getresponse()文件/usr/lib/python2.6/httplib.py",第 950 行,在 getresponseresponse.begin()文件/usr/lib/python2.6/httplib.py",第 390 行,在开始版本、状态、原因 = self._read_status()文件/usr/lib/python2.6/httplib.py",第 354 行,在 _read_status提高 BadStatusLine(line)httplib.BadStatusLine

有人知道什么是错误状态行"吗?

我在很多服务器和很多 URL 上都尝试过这个,但我仍然收到这个错误?

解决方案

来自 的文档httplib (Python 2)(在 Python 3 中称为 http.clienta>):

<块引用>

exception httplib.BadStatusLine: (exception http.BadStatusLine:)

<块引用>

HTTPException 的子类.

如果服务器以我们不理解的 HTTP 状态代码响应,则引发.

我运行了相同的代码并且没有收到错误:

<预><代码>>>>theurl = 'http://www.garageband.com/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3'>>>如果 url.startswith("http://"):... theurl = theurl[7:]... head = theurl[:theurl.find('/')]... tail = theurl[theurl.find('/'):]...>>>头'www.garageband.com'>>>尾巴'/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3'>>>response_code = 0>>>导入 httplib>>>conn = httplib.HTTPConnection(head)>>>conn.request("HEAD", 尾)>>>res = conn.getresponse()>>>状态302>>>response_code = int(res.status)

我想只需仔细检查所有内容然后再试一次?

if theurl.startswith("http://"): theurl = theurl[7:]
    head = theurl[:theurl.find('/')]
    tail = theurl[theurl.find('/'):]
response_code = 0
import httplib
conn = httplib.HTTPConnection(head)
conn.request("HEAD",tail)
res = conn.getresponse()
response_code = int(res.status)

http://www.garageband.com/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3
Traceback (most recent call last):
  File "check_data_404.py", line 51, in <module>
    run()
  File "check_data_404.py", line 35, in run
    res = conn.getresponse()
  File "/usr/lib/python2.6/httplib.py", line 950, in getresponse
    response.begin()
  File "/usr/lib/python2.6/httplib.py", line 390, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.6/httplib.py", line 354, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine

Does anyone know what "Bad Status Line" is?

Edit: I tried this for many servers, and many URL's and I still get this error?

解决方案

From the documentation for httplib (Python 2) (called http.client in Python 3):

exception httplib.BadStatusLine: (exception http.client.BadStatusLine:)

A subclass of HTTPException.

Raised if a server responds with an HTTP status code that we don’t understand.

I ran the same code and did not receive an error:

>>> theurl = 'http://www.garageband.com/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3'
>>> if theurl.startswith("http://"):
...     theurl = theurl[7:]
...     head = theurl[:theurl.find('/')]
...     tail = theurl[theurl.find('/'):]
... 
>>> head
'www.garageband.com'
>>> tail
'/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3'
>>> response_code = 0
>>> import httplib
>>> conn = httplib.HTTPConnection(head)
>>> conn.request("HEAD", tail)
>>> res = conn.getresponse()
>>> res.status
302
>>> response_code = int(res.status)

I guess just double-check everything and try again?

这篇关于为什么我在 python 中收到这个错误?(httplib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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