#python #ssl #python-requests
Вопрос:
Я пытаюсь получить некоторые комментарии со страницы URL, она хорошо работала несколько дней назад и не работала со вчерашнего дня. Я отправил скрипт python своим друзьям, они могли запустить тот же файл python на своем mac, но на моем собственном mac произошла эта ошибка.
Traceback (most recent call last):
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 485, in wrap_socket
cnx.do_handshake()
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1934, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1663, in _raise_ssl_error
raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (54, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 672, in urlopen
chunked=chunked,
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 376, in _make_request
self._validate_conn(conn)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 994, in _validate_conn
conn.connect()
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/connection.py", line 360, in connect
ssl_context=context,
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 370, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 491, in wrap_socket
raise ssl.SSLError("bad handshake: %r" % e)
ssl.SSLError: ("bad handshake: SysCallError(54, 'ECONNRESET')",)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/connectionpool.py", line 720, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/urllib3/util/retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='appweb.lizhi.fm', port=443): Max retries exceeded with url: /live/comments?liveId=5188859099674684982amp;start=1625957008136amp;count=50 (Caused by SSLError(SSLError("bad handshake: SysCallError(54, 'ECONNRESET')")))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "lizhi_alert.py", line 42, in <module>
res = requests.get(url, headers = headers, verify = False, timeout = 5)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/Users/sherry/.pyenv/versions/version376env/lib/python3.7/site-packages/requests/adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='appweb.lizhi.fm', port=443): Max retries exceeded with url: /live/comments?liveId=5188859099674684982amp;start=1625957008136amp;count=50 (Caused by SSLError(SSLError("bad handshake: SysCallError(54, 'ECONNRESET')")))
Этот код хорошо работал несколько дней назад, и я не знал, почему он внезапно не заработал на моем mac. Я попытался изменить свой ip-адрес, проигнорировать проверку ssl и добавить заголовки, но также потерпел неудачу.
Заранее спасибо за вашу помощь!
url=f"https://appweb.lizhi.fm/live/comments?liveId={liveid}amp;start={msg_time}amp;count=50"
headers = {'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"}
res = requests.get(url, timeout = 5, headers = headers,verify = False)
try:
json_res = res.json()
except Exception as e:
print(f"Failed to read comments: {e}")
print(res.text)
continue
Комментарии:
1. Скорее всего, вы были заблокированы, также может быть далеко не идеальная реализация TLS на сервере .