Get Errno from Python Requests ConnectionError?

后端 未结 2 2036
傲寒
傲寒 2021-02-03 10:57

I\'m catching and printing Python Requests ConnectionErrors fine with just this:

except requests.exceptions.ConnectionError as e:
    logger.warning(str(e.messag         


        
2条回答
  •  失恋的感觉
    2021-02-03 11:26

    I had troubles getting the same results with python 3.6 and requests 2.18. I managed to get the errno using the http and socket modules :

    import socket, html
    try:
        http.client.HTTPConnection('invalid').connect()
    except (socket.gaierror, ConnectionError) as e:
        print(e.errno)
    

    Hopefully it helps someonelse.

提交回复
热议问题