‘Connection aborted.’, RemoteDisconnected(‘Remote end closed connection without response’)

huangapple go评论90阅读模式
英文:

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

问题

我正在尝试让本地前端Flask服务器向本地后端服务器发送POST请求。两个服务器都在同一IP和网络上以不同的端口运行,后端使用http库。

我想从前端发送一个POST请求到后端,然后后端返回状态码200。但我收到了这个错误!

以下是我的前端代码:

from flask import *
import requests

HOST = "localhost"
PORT = 7000

app = Flask(__name__)

@app.route("/")
def main():
    return render_template('home.html')

@app.route("/login")
def login():
    return render_template('index.html')

@app.route("/login_submit", methods=["POST"])
def login_submit():
    response = send_credentials_to_backend()
    if response == 200:
        return redirect(url_for('main'))
    else:
        return "Error: Failed to authenticate"

def send_credentials_to_backend():
    url = f"http://{HOST}:8000/login/submit"
    response = requests.post(url)
    response.raise_for_status()
    return response.status_code

if __name__ == "__main__":
    app.run(host=HOST, port=PORT, debug=True)

以下是我的后端:

from http.server import *
import json

HOST = "localhost"
PORT = 8000

class HTTPServerClient(BaseHTTPRequestHandler):

    def do_POST(self):
        print("Got request POST")
        if self.path.endswith('/login/submit'):
            self.send_response_only(200, message=None)

    def do_GET(self):
        print("Got request GET")
        self.send_response_only(200, message=None)

server = HTTPServer((HOST, PORT), HTTPServerClient)

print("Back-End running...")

server.serve_forever()
server.server_close()

以下是我的日志:

Traceback (most recent call last):
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connection.py", line 454, in getresponse
    httplib_response = super().getresponse()
  File "C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1377, in getresponse
    response.begin()
  File "C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 320, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 289, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen
    retries = retries.increment(
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\util\retry.py", line 470, in increment
    raise reraise(type(error), error, _stacktrace)
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\util\util.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connection.py", line 454, in getresponse
    httplib_response = super().getresponse()
  File "C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1377, in getresponse
    response.begin()
  File "C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 320, in begin
    version, status, reason = this.read_status()
  File "C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 289, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py", line 2213, in __call__
    return this.wsgi_app(environ, start_response)
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py", line 2193, in wsgi_app
    response = this.handle_exception(e)
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py", line 2190, in wsgi_app
    response = this.full_dispatch_request()
  File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py", line 1486, in full_dispatch_request
    rv = this.handle_user_exception(e)
  File "C:\Users\Monkata

<details>
<summary>英文:</summary>

Im trying to make a local frontend flask server send a POST request to my local backend server. Both servers are running on python and on the same IP and network with different ports. The backend uses the http library.

I want to send a POST request from my frontend to my backend and then the backend to retunt status code 200.But i get this error!

Here is my frontend code:


from flask import *
import requests

HOST = "localhost"
PORT = 7000

app = Flask(name)

@app.route("/")
def main():
return render_template('home.html')

@app.route("/login")
def login():
return render_template('index.html')

@app.route("/login_submit", methods=["POST"])
def login_submit():

response = send_credentials_to_backend()
if response == 200:
    return redirect(url_for(&#39;main&#39;))
else:
    return &quot;Error: Failed to authenticate&quot;

def send_credentials_to_backend():

url = f&quot;http://{HOST}:8000/login/submit&quot;
response = requests.post(url)
response.raise_for_status()
return response.status_code

if name == "main":
app.run(host=HOST, port=PORT, debug=True)


Here is my backend:

from http.server import *
import json

HOST = "localhost"
PORT = 8000

class HTTPServerClient(BaseHTTPRequestHandler):

def do_POST(self):
    print(&quot;Got request POST&quot;)
    if self.path.endswith(&#39;/login/submit&#39;):
        self.send_response_only(200, message=None)

def do_GET(self):
    print(&quot;Got request GET&quot;)
    self.send_response_only(200, message=None)
    print(self.client_address)

server = HTTPServer((HOST, PORT), HTTPServerClient)

print("Back-End running...")

server.serve_forever()
server.server_close()


Here is my log:

Traceback (most recent call last):
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py&quot;, line 790, in urlopen
    response = self._make_request(
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py&quot;, line 536, in _make_request
    response = conn.getresponse()
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connection.py&quot;, line 454, in getresponse
    httplib_response = super().getresponse()
  File &quot;C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py&quot;, line 1377, in getresponse
    response.begin()
  File &quot;C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py&quot;, line 320, in begin
    version, status, reason = self._read_status()
  File &quot;C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py&quot;, line 289, in _read_status
    raise RemoteDisconnected(&quot;Remote end closed connection without&quot;
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\adapters.py&quot;, line 486, in send
    resp = conn.urlopen(
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py&quot;, line 844, in urlopen
    retries = retries.increment(
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\util\retry.py&quot;, line 470, in increment
    raise reraise(type(error), error, _stacktrace)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\util\util.py&quot;, line 38, in reraise
    raise value.with_traceback(tb)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py&quot;, line 790, in urlopen
    response = self._make_request(
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connectionpool.py&quot;, line 536, in _make_request
    response = conn.getresponse()
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\urllib3\connection.py&quot;, line 454, in getresponse
    httplib_response = super().getresponse()
  File &quot;C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py&quot;, line 1377, in getresponse
    response.begin()
  File &quot;C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py&quot;, line 320, in begin
    version, status, reason = self._read_status()
  File &quot;C:\Users\Monkata\AppData\Local\Programs\Python\Python39\lib\http\client.py&quot;, line 289, in _read_status
    raise RemoteDisconnected(&quot;Remote end closed connection without&quot;
urllib3.exceptions.ProtocolError: (&#39;Connection aborted.&#39;, RemoteDisconnected(&#39;Remote end closed connection without response&#39;))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py&quot;, line 2213, in __call__
    return self.wsgi_app(environ, start_response)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py&quot;, line 2193, in wsgi_app
    response = self.handle_exception(e)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py&quot;, line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py&quot;, line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py&quot;, line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py&quot;, line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\front-end\app.py&quot;, line 23, in login_submit
    response = send_credentials_to_backend()
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\front-end\app.py&quot;, line 33, in send_credentials_to_backend
    response = requests.post(url)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\api.py&quot;, line 115, in post
    return request(&quot;post&quot;, url, data=data, json=json, **kwargs)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\api.py&quot;, line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\sessions.py&quot;, line 589, in request
    resp = self.send(prep, **send_kwargs)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\sessions.py&quot;, line 703, in send
    r = adapter.send(request, **kwargs)
  File &quot;C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\adapters.py&quot;, line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: (&#39;Connection aborted.&#39;, RemoteDisconnected(&#39;Remote end closed connection without response&#39;))

I set the message=None in the backend when it returns 200 but that din&#39;t work.
I tried with both POST and GET requests. That din&#39;t work.
I removed any paylod when sending the request. Also din&#39;t work.
Any ideas?

</details>


# 答案1
**得分**: 0

将以下内容翻译成中文:

```python
做这个:

     self.send_response(200)
     self.send_header("Content-type", "text/html")
     self.end_headers()

而不是这个:

    self.send_response(200, message=None)

在后端!
英文:

Do this:

 self.send_response(200)
 self.send_header(&quot;Content-type&quot;, &quot;text/html&quot;)
 self.end_headers()

Instead of this:

self.send_response(200, message=None)

In the backend!

huangapple
  • 本文由 发表于 2023年6月9日 02:21:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定