英文:
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('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)
Here is my backend:
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)
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 "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 = 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"
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 self.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 = self.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 = self.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 = self.handle_user_exception(e)
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py", line 1484, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\flask\app.py", line 1469, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\front-end\app.py", line 23, in login_submit
response = send_credentials_to_backend()
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\front-end\app.py", line 33, in send_credentials_to_backend
response = requests.post(url)
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\api.py", line 115, in post
return request("post", url, data=data, json=json, **kwargs)
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Monkata\Documents\PyCharm Projects\TEST WORKING SITE\venv\lib\site-packages\requests\adapters.py", line 501, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
I set the message=None in the backend when it returns 200 but that din't work.
I tried with both POST and GET requests. That din't work.
I removed any paylod when sending the request. Also din'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("Content-type", "text/html")
self.end_headers()
Instead of this:
self.send_response(200, message=None)
In the backend!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论