xml获取使用请求库导致异常

时间:2017-05-15 00:27:12

标签: xml python-3.x python-requests

我是Python编程的新手,并且已经尝试过研究我的问题但是还没有找到解决方案。我希望有人可以提供帮助。

我有一个脚本可以对一个用于激活或停用Web中继交换机的IP地址进行http调用。代码的相对部分如下所示:

import requests
r = requests.get('http://192.168.1.100/state.xml?relayState=1&noReply=0')

以上成功打开了Web中继交换机,但来自Web中继的响应导致了许多异常消息,如下所示,脚本停止执行。我不需要为我的脚本处理任何响应,但如果我需要为了使脚本正常运行,我会。我的预感是问题围绕着使用xml版本1.0而不是1.1的Web中继交换机。我还没有能够强制要求使用1.0的方式。我正在使用Python 3.6.1

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 386, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 382, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 279, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: <?xml version='1.0' encoding='utf-8'?>



During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\adapters.py", line 423, in send
    timeout=timeout
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 649, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\util\retry.py", line 347, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 386, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 382, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 1331, in getresponse
    response.begin()
  File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Program Files (x86)\Python36-32\lib\http\client.py", line 279, in _read_status
    raise BadStatusLine(line)
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine("<?xml version='1.0' encoding='utf-8'?>\r\n",))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Software/Python/test3.py", line 3, in <module>
    r = requests.get('http://192.168.1.100/state.xml?relayState=1')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\requests\adapters.py", line 473, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("<?xml version='1.0' encoding='utf-8'?>\r\n",))
>>> 

2 个答案:

答案 0 :(得分:2)

您确定网址应该是http而不是https吗?这是某人的问题:Python requests.exception.ConnectionError: connection aborted "BadStatusLine"

否则,该异常意味着错误是来自服务器的响应,我假设您无法控制。您可以与控制它的人一起打开支持票,或者,如果您赶时间,可以通过捕获并忽略异常来解决它,这很麻烦但是您可能会接受,因为您不需要响应并且知道你的请求在另一端是成功的:

let

对于生产代码,我想要了解错误的底部,但在开发期间,我可能愿意做类似以下的事情,所以我可以继续前进。

答案 1 :(得分:0)

我是上面列出的卷筒继电器开关产品制造商的技术支持专家。最近一封支持电子邮件提到了这个帖子,我认为为其他任何可能遇到此问题的人添加信息会很好。

很久以前设计的几个较旧的模块具有有限的硬件资源,并且不支持XML响应的完整HTML标头。

几年前,这些产品使用较新的硬件进行了重新设计。出于向后兼容性原因,/ state.xml响应仍然没有完整的HTML标头,以适应那些为旧硬件系列开发软件的人。

新产品现在支持完整的HTML标头,但必须提出不同的请求 - /stateFull.xml。

原始示例的正确代码如下:

import requests
r = requests.get('http://192.168.1.100/stateFull.xml?relayState=1&noReply=0')

注意:首次提交到StackOverflow,所以我为缺少通常遵循的任何约定而道歉。