0

I'm trying to validate some urls that are passed to me. I want to make sure that they resolve to an existing website. In the process, I've coded:

try:
   logoStatus = requests.request('GET', logo)
   feedStatus = requests.request('GET', feed)
except requests.exceptions.ConnectionError:

Rather than stopping at the exception handler, the code stops and displays the following:

Exception has occurred: ConnectionError HTTPConnectionPool(host='some.web.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f33d102f978>: Failed to establish a new connection: [Errno -2] Name or service not known',))

I referred to Correct way to try/except using Python requests module? in trying to code this, but, it doesn't seem to work for me.

Can someone point out what I'm doing wrong?

rrirower
  • 4,338
  • 4
  • 27
  • 45
  • Works fine for me. try the following code to interact with your exception `try: logoStatus = requests.request('GET', logo) except Exception as e: print(type(e)) # or set a breakpoint and investigate` Alternatively - provide some extra details (what url, python version, requests version, etc) – Y.R. Jun 28 '21 at 19:48
  • @Y.R. Thanks for the response. I'm using python3 (3.6) and requests 2.25.1. I'm debugging in Visual Studio Code. No matter what I try, the ConnectionError is ignored. The url is not a valid url. That's the purpose of this code. I'm trying to warn the user that he has entered a url that does not resolve to an actual site. – rrirower Jun 30 '21 at 14:36
  • I dug deeper. This is a problem with the way Visual Studio code handles this exception. I ran the code external to VSC and it ran successfully and trapped the exception. – rrirower Jun 30 '21 at 14:54
  • Makes sense. I tested it using python in my cmd. You should add a comment describing the solution, other people may encounter the same thing – Y.R. Jun 30 '21 at 22:34

0 Answers0