当访问https站点时,requests.get()会引发错误,而访问http站点时,则会提供html输出

时间:2019-10-22 06:07:18

标签: api python-requests pycharm

下面是我要为api测试运行的程序。 m通过pycharm ide最新版本的selenium / python运行它

import requests
import json
import jsonpath
url = "https://jsonplaceholder.typicode.com/posts"
grsp = requests.get(url)
print(grsp.content)

我得到的错误是:

Traceback (most recent call last):
  File "python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 660, in urlopen
    chunked=chunked,
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 371, in _make_request
    self._validate_conn(conn)
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 982, in _validate_conn
    conn.connect()
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connection.py", line 336, in connect
    conn = self._new_conn()
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connection.py", line 171, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x000000000372A438>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "\PycharmProjects\API\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\connectionpool.py", line 708, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "C:\python\lib\site-packages\urllib3-1.25.3-py3.7.egg\urllib3\util\retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='jsonplaceholder.typicode.com', port=443): Max retries exceeded with url: /posts (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000000000372A438>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/PycharmProjects/API/get/get.py", line 7, in <module>
    grsp = requests.get(url)
  File "\PycharmProjects\API\venv\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "\PycharmProjects\API\venv\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "\PycharmProjects\API\venv\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "\PycharmProjects\API\venv\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "\PycharmProjects\API\venv\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='jsonplaceholder.typicode.com', port=443): Max retries exceeded with url: /posts (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000000000372A438>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))

相同的api在Soapui和post man中都可以正常工作。而且我尝试通过pycharm的所有API都存在此错误。可能是什么错误?

0 个答案:

没有答案