title: python capture URLError code
body: <p>I want to use Python to monitor a website that uses HTTPS.
  The problem is that the certificate on the website is invalid.
  I don't care about that, I just want to know that the website is running.</p>
  
  <p>My working code looks like this:</p>
  
  <pre><code>from urllib.request import Request, urlopen
  from urllib.error import URLError, HTTPError
  req = Request("https://somedomain.com")
  
  try:
      response = urlopen(req)
  except HTTPError as e:
      print('server couldn\'t fulfill the request')
      print('error code: ', e.code)
  except URLError as e:
          print(e.args)       
  else:
      print ('website ok')
  </code></pre>
  
  <p>that ends in URLError being called. The error code is 645.</p>
  
  <pre><code>C:\python&gt;python monitor443.py
  (SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)
  </code></pre>
  
  <p>So, I'm trying to except code 645 as OK. I've tried this:</p>
  
  <pre><code>from urllib.request import Request, urlopen
  from urllib.error import URLError, HTTPError
  req = Request("https://somedomain.com")
  
  try:
      response = urlopen(req)
  except HTTPError as e:
      print('server couldn\'t fulfill the request')
      print('error code: ', e.code)
  except URLError as e:
          if e.code == 645:
              print("ok")
          print(e.args)       
  else:
      print ('website ok')
  </code></pre>
  
  <p>but get this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "monitor443.py", line 11, in &lt;module&gt;
      if e.code == 645:
  AttributeError: 'URLError' object has no attribute 'code'
  </code></pre>
  
  <p>how do I add this exception?</p>