Я выполняю запрос на получение IP-адресов с веб-сайта, но в половине случаев продолжаю сталкиваться с проблемой, когда выдает следующую ошибку. Он говорит мне, что мой SSL - неправильная версия, я поискал по всему Интернету, но не нашел ничего полезного. через l oop, и это вызывает ошибку ниже.
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'
Ошибка:
---------------------------------------------------------------------------
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)>