POST请求localhost Python

时间:2018-02-14 18:19:16

标签: python python-requests

我想测试一下我使用localhost创建的API。

import requests

def test_post_spread_api():
    r = requests.post("https://localhost:8000/api/test")
    print(r.status_code)

test_post_spread_api()

然而,我收到错误:

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/misc/spread_monitor (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f8298604940>: Failed to establish a new connection: [Errno 111] Connection refused',))

API没有任何问题,因为它在我使用邮递员时起作用。 我正在使用带有django框架的docker运行我的localhost。有什么想法吗?

编辑:完成追溯:

Traceback (most recent call last):
  File "sandbox/test_spread_monitor.py", line 12, in <module>
    test_post_spread_api()
  File "sandbox/test_spread_monitor.py", line 9, in test_post_spread_api
    r = requests.post("https://localhost:8000/api/test")
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 508, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/misc/spread_monitor (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f2fb9772908>: Failed to establish a new connection: [Errno 111] Connection refused',))

1 个答案:

答案 0 :(得分:-1)

尝试将 "https://localhost:8000/api/test" 更改为 "http://localhost:8000/api/test"