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

时间:2017-12-20 16:44:46

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

我有一个简单的函数,它将XML字符串发送到URL并进行响应。我不知道如何处理URL无效的情况。我试图把“URLError”和“socket.gaierror”(两者都显示在错误输出中),但似乎都没有捕获无效的URL错误。

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块的原始错误输出:

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>

1 个答案:

答案 0 :(得分:2)

╔════════════════════════════════════════════════════════════════╗
║     Claymore's Dual ETH + DCR/SC/LBC/PASC GPU Miner v10.0      ║
╚════════════════════════════════════════════════════════════════╝

ETH: 5 pools are specified
Main Ethereum pool is eth-asia1.nanopool.org:9999
At least 16 GB of Virtual Memory is required for multi-GPU systems
Make sure you defined GPU_MAX_ALLOC_PERCENT 100
Be careful with overclocking, use default clocks for first tests
Press "s" for current statistics, "0".."9" to turn on/off cards, "r" to reload pools, "e" or "d" to select current pool
OpenCL initializing...

AMD Cards available: 1
GPU #0: Hainan, 2048 MB available, 5 compute units
POOL/SOLO version
GPU #0: set -etha as 1 (ETH algo for slow cards)
No NVIDIA CUDA GPUs detected.
Total cards: 1
ETH: Stratum - connecting to 'eth-asia1.nanopool.org' <139.99.102.73> port 9999
ETHEREUM-ONLY MINING MODE ENABLED (-mode 1)
ETH: eth-proxy stratum mode
Watchdog enabled
Remote management (READ-ONLY MODE) is enabled on port 3333

ETH: Stratum - Connected (eth-asia1.nanopool.org:9999)
ETH: Authorized
Setting DAG epoch #159...
Setting DAG epoch #159 for GPU0
Create GPU buffer for GPU0
GPU0 - not enough GPU memory to place DAG, you cannot mine this coin with this GPU
GPU0 - OpenCL error -61 - cannot allocate big buffer for DAG. Check readme.txt for possible solutions.
Setting DAG epoch #159 for GPU0
GPU 0 failed
GPU0, OpenCL error -38 - cannot write buffer for DAG

并使用GPU_FORCE_64BIT_PTR 0 GPU_MAX_HEAP_SIZE 100 GPU_USE_SYNC_OBJECTS 1 GPU_MAX_ALLOC_PERCENT 100 GPU_SINGLE_ALLOC_PERCENT 100 EthDcrMiner64.exe -epool eth-asia1.nanopool.org:9999 -ewal <my address link> -epsw x -mode 1 -ftime 10

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