首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我将XML发送到无效的URL时,如何捕获错误?(Python)

当我将XML发送到无效的URL时,如何捕获错误?(Python)
EN

Stack Overflow用户
提问于 2017-12-21 00:44:46
回答 1查看 230关注 0票数 0

我有一个简单的函数,可以将XML字符串发送到URL并接受响应。我不知道如何处理URL无效的情况。我尝试输入"URLError“和"socket.gaierror”(它们都显示在错误输出中),但似乎都没有捕捉到无效的URL错误。

代码语言:javascript
复制
import urllib.request as request

def getResponse(<params>):
    xmlReq = "an XML string"

    #make a new request
    new_req = request.Request(url="<an invalid URL>", 
              data=xmlReq)        

    try:
        xml_response = request.urlopen(new_req)
    except <not sure what error goes here to catch invalid URL>:
        print("Invalid URL. Aborting process.")
        quit()

这是不带try-except块的原始错误输出:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\<python storage location>\Python\Python36-32\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\<python storage location>\Python\Python36-32\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\<python storage location>\Python\Python36-32\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\<python storage location>\Python\Python36-32\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\<python storage location>\Python\Python36-32\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\<python storage location>\Python\Python36-32\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\<python storage location>\Python\Python36-32\lib\http\client.py", line 1392, in connect
    super().connect()
  File "C:\<python storage location>\Python\Python36-32\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\<python storage location>\Python\Python36-32\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\<python storage location>\Python\Python36-32\lib\socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\<my workspace>\XMLReq.py", line ##, in <module>
    getResponse(<params>)
  File "C:\<my workspace>\XMLReq.py", line ##, in getResponse
    xml_response = request.urlopen(new_req)
  File "C:\<python storage location>\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\<python storage location>\Python\Python36-32\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "C:\<python storage location>\Python\Python36-32\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "C:\<python storage location>\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\<python storage location>\Python\Python36-32\lib\urllib\request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\<python storage location>\Python\Python36-32\lib\urllib\request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-21 00:53:42

代码语言:javascript
复制
import urllib

并使用urllib.error.URLError

请参阅此处的参考:https://docs.python.org/3/library/urllib.error.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47910752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档