【问题标题】:Hackerrank Python Requests ConnectionErrorHackerrank Python 请求 ConnectionError
【发布时间】:2022-08-02 22:17:12
【问题描述】:

我有一个 HackerRank REST API 挑战,我使用 Python 向指定的 hackerrank api 发出请求,但在 HackerRank 中,它一直(并且一直)引发 requests.exceptions.ConnectionError。 我已将任何 HackerRank 挑战编辑器窗口中的错误隔离到这个最小的测试代码中:

import requests
response = requests.get(\"https://jsonmock.hackerrank.com/api/moviesdata\")
print(response)

这在我的计算机上可以正常工作,给出 200 响应状态代码,但从 Hackerrank 中它给出了这个运行时错误。 知道hackerrank是否会阻止连接吗?还是我应该使用任何其他 python 库?

Traceback (most recent call last):
  File \"/usr/local/site-packages/urllib3/connection.py\", line 157, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File \"/usr/local/site-packages/urllib3/util/connection.py\", line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File \"/usr/local/lib-python/3/socket.py\", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
_socket.gaierror: [Errno -3] Temporary failure in name resolution
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File \"/usr/local/site-packages/urllib3/connectionpool.py\", line 672, in urlopen
    chunked=chunked,
  File \"/usr/local/site-packages/urllib3/connectionpool.py\", line 376, in _make_request
    self._validate_conn(conn)
  File \"/usr/local/site-packages/urllib3/connectionpool.py\", line 994, in _validate_conn
    conn.connect()
  File \"/usr/local/site-packages/urllib3/connection.py\", line 334, in connect
    conn = self._new_conn()
  File \"/usr/local/site-packages/urllib3/connection.py\", line 169, in _new_conn
    self, \"Failed to establish a new connection: %s\" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x0000000001f00bb8>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File \"/usr/local/site-packages/requests/adapters.py\", line 449, in send
    timeout=timeout
  File \"/usr/local/site-packages/urllib3/connectionpool.py\", line 720, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File \"/usr/local/site-packages/urllib3/util/retry.py\", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host=\'jsonmock.hackerrank.com\', port=443): Max retries exceeded with url: /api/moviesdata (Caused by NewConnectionError(\'<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000001f00bb8>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution\',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File \"Solution.py\", line 2, in <module>
  File \"/usr/local/site-packages/requests/api.py\", line 75, in get
    return request(\'get\', url, params=params, **kwargs)
  File \"/usr/local/site-packages/requests/api.py\", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File \"/usr/local/site-packages/requests/sessions.py\", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File \"/usr/local/site-packages/requests/sessions.py\", line 646, in send
    r = adapter.send(request, **kwargs)
  File \"/usr/local/site-packages/requests/adapters.py\", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host=\'jsonmock.hackerrank.com\', port=443): Max retries exceeded with url: /api/moviesdata (Caused by NewConnectionError(\'<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000001f00bb8>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution\',))
  • 你看到错误信息了吗? \"名称解析暂时失败\" ---hackerrank 有 DNS 问题。你对此无能为力。稍后再试。

标签: python connection runtime-error


【解决方案1】:

这应该适用于您的场景(响应为 JSON 格式):

import requests
response = requests.get("https://jsonmock.hackerrank.com/api/moviesdata")
print(response.json())

回复:

{'page': 1, 'per_page': 10, 'total': 866, 'total_pages': 87, 'data': [{'Title': 'Waterworld', 'Year': 1995, 'imdbID': 'tt0114898'}, {'Title': 'Waterworld', 'Year': 1995, 'imdbID': 'tt0189200'}, {'Title': "The Making of 'Waterworld'", 'Year': 1995, 'imdbID': 'tt2670548'}, {'Title': 'Waterworld 4: History of the Islands', 'Year': 1997, 'imdbID': 'tt0161077'}, {'Title': 'Waterworld', 'Year': 1997, 'imdbID': 'tt0455840'}, {'Title': 'Italian Spiderman', 'Year': 2007, 'imdbID': 'tt2705436'}, {'Title': 'Superman, Spiderman or Batman', 'Year': 2011, 'imdbID': 'tt2084949'}, {'Title': 'Spiderman', 'Year': 1990, 'imdbID': 'tt0100669'}, {'Title': 'Fighting, Flying and Driving: The Stunts of Spiderman 3', 'Year': 2007, 'imdbID': 'tt1132238'}, {'Title': 'Spiderman 5', 'Year': 2008, 'imdbID': 'tt3696826'}]}

另外,请确保使用pip install -U requests 更新您的请求包

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多