0

I am running a request to get IP addresses from a website but keep running into a problem half of the time where it gives the following error. It tells me that my SSL is the wrong version, I have searched all over the web but haven't found anything helpful.

What happens is that I am looping over a list of IP addresses and I get about halfway through the loop and it throws the error below.

def ipInfo(addr=''):
    try:
        url = f'https://tools.keycdn.com/geo?host={addr}'
    except:
        return 'Error'
    #f = requests.get(url, verify=False, ssl)
    x = urllib.request.urlopen(url, context=ssl.SSLContext())
    #c = str(f.content)
    c = str(x.read())
    number = re.compile('Country</dt><dd class="col-8 text-monospace">')
    attempt = number.search(c)
    if attempt is not None:
        attempt = attempt.span()
        l = list(attempt)
        val = c[l[1]:l[1]+25].split('<')[0]
        return val
    else:
        return 'No country associated'

Error:

---------------------------------------------------------------------------
SSLError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
   1318                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1319                           encode_chunked=req.has_header('Transfer-encoding'))
   1320             except OSError as err: # timeout error

C:\ProgramData\Anaconda3\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
   1251         """Send a complete request to the server."""
-> 1252         self._send_request(method, url, body, headers, encode_chunked)
   1253 

C:\ProgramData\Anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1297             body = _encode(body, 'body')
-> 1298         self.endheaders(body, encode_chunked=encode_chunked)
   1299 

C:\ProgramData\Anaconda3\lib\http\client.py in endheaders(self, message_body, encode_chunked)
   1246             raise CannotSendHeader()
-> 1247         self._send_output(message_body, encode_chunked=encode_chunked)
   1248 

C:\ProgramData\Anaconda3\lib\http\client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

C:\ProgramData\Anaconda3\lib\http\client.py in send(self, data)
    965             if self.auto_open:
--> 966                 self.connect()
    967             else:

C:\ProgramData\Anaconda3\lib\http\client.py in connect(self)
   1421             self.sock = self._context.wrap_socket(self.sock,
-> 1422                                                   server_hostname=server_hostname)
   1423 

C:\ProgramData\Anaconda3\lib\ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
    422             context=self,
--> 423             session=session
    424         )

C:\ProgramData\Anaconda3\lib\ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)
    869                         raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
--> 870                     self.do_handshake()
    871             except (OSError, ValueError):

C:\ProgramData\Anaconda3\lib\ssl.py in do_handshake(self, block)
   1138                 self.settimeout(None)
-> 1139             self._sslobj.do_handshake()
   1140         finally:

SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076)

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
<ipython-input-198-9aba5213355b> in <module>
      2 d = {}
      3 for x in IPs:
----> 4     d[x] = ipInfo(x)
      5 finish = time.time()
      6 print(finish - start)

<ipython-input-197-7e56c6097581> in ipInfo(addr)
      5         return 'Error'
      6     #f = requests.get(url, verify=False, ssl)
----> 7     x = urllib.request.urlopen(url, context=ssl.SSLContext())
      8     #c = str(f.content)
      9     c = str(x.read())

C:\ProgramData\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

C:\ProgramData\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
    523             req = meth(req)
    524 
--> 525         response = self._open(req, data)
    526 
    527         # post-process response

C:\ProgramData\Anaconda3\lib\urllib\request.py in _open(self, req, data)
    541         protocol = req.type
    542         result = self._call_chain(self.handle_open, protocol, protocol +
--> 543                                   '_open', req)
    544         if result:
    545             return result

C:\ProgramData\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

C:\ProgramData\Anaconda3\lib\urllib\request.py in https_open(self, req)
   1360         def https_open(self, req):
   1361             return self.do_open(http.client.HTTPSConnection, req,
-> 1362                 context=self._context, check_hostname=self._check_hostname)
   1363 
   1364         https_request = AbstractHTTPHandler.do_request_

C:\ProgramData\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
   1319                           encode_chunked=req.has_header('Transfer-encoding'))
   1320             except OSError as err: # timeout error
-> 1321                 raise URLError(err)
   1322             r = h.getresponse()
   1323         except:

URLError: <urlopen error [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076)>

1 Answers1

0

The web-page being queried states

We are rate limiting requests (3r/s) to avoid overload of the system

and your stacktrace output makes multiple references to request time-outs so I'd say the error is likely due to the web-app temporarily blocking you due to the number of requests being submitted.

You could test this theory by making use of the urllib.request.urlopen() function's optional timeout parameter;

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

smoggers
  • 3,154
  • 6
  • 26
  • 38
  • I added a timeout but now instead, I get a timeout error that says, "The read operation timed out" – Noah Schwarzkopf Aug 05 '20 at 20:39
  • Understood, maybe try different values for the timeout see if that has any effect. Also, try making a request for just one IP to see if that works (perhaps the server blocks access from your IP for a specific time-period after making too many requests). I'll delete this answer if this turns out to be of no use – smoggers Aug 06 '20 at 09:25