首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >连接中止: RemoteDisconnected (“没有响应的远程结束关闭连接”)在发送邮件时大约5分钟不活动之后

连接中止: RemoteDisconnected (“没有响应的远程结束关闭连接”)在发送邮件时大约5分钟不活动之后
EN

Stack Overflow用户
提问于 2022-05-26 14:42:39
回答 1查看 5.1K关注 0票数 -3

我把空闲超时设置为60分钟。但是,在使用Python客户机与Python服务器对话的情况下,当发送POST时,连接通过对等方重置约5分钟的不活动时间。即使在连接被重置或断开之后,该活动仍在服务器端发生。

代码语言:javascript
复制
import requests
import logging
import flask
from flask import Flask

app = Flask(__name__)
logging.basicConfig(level=logging.INFO, format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')

@app.before_first_request
def before_first_request():
    app.logger.info("before_first_request")

@app.before_request
def before_request():
    app.logger.info("before_request")

@app.route('/run', methods=['POST'])
def run(redirect_url="",additional_testng_parameters=""):
    url = "http://192.168.1.8:7070/" + "run"
    app.logger.info("url is:" + url)
   
    session = requests.Session()   
    app.logger.info("Request placed for " + url)
    response = "Text"
    try:
        response = session.post(url=url, timeout=3600)
        resultdata = response.headers['Content-Type']
        app.logger.info("Response received for : " + url + " is:" + str(response.status_code))
        app.logger.info("Response received for : " + url + " is:" + str(resultdata))
        response = make_response("Execution is successful")
    except Exception as err:
        app.logger.error(err)
    return response

@app.errorhandler(Exception)
def server_error(err):
    app.logger.exception(err)
    return "There is some error on Test Invoker side", 500

if __name__ == '__main__':
    from waitress import serve
    serve(app, host="0.0.0.0", port=8080)

大约在5分钟后得到以下错误消息

代码语言:javascript
复制
ERROR app waitress-0 : ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

以下是一些参考资料,但并不能解决我的问题。

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

https://www.pythonfixing.com/2022/01/fixed-aborted-remotedisconnected-end.html

下面的代码帮助我解决了这个问题

代码语言:javascript
复制
import socket
from urllib3.connection import HTTPConnection

HTTPConnection.default_socket_options = (
    HTTPConnection.default_socket_options + [
        (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
        (socket.SOL_TCP, socket.TCP_KEEPIDLE, 45),
        (socket.SOL_TCP, socket.TCP_KEEPINTVL, 10),
        (socket.SOL_TCP, socket.TCP_KEEPCNT, 6)
    ]
)
EN

回答 1

Stack Overflow用户

发布于 2022-06-07 08:02:53

试试这个:

代码语言:javascript
复制
import socket
from urllib3.connection import HTTPConnection

HTTPConnection.default_socket_options = (
    HTTPConnection.default_socket_options + [
        (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
        (socket.SOL_TCP, socket.TCP_KEEPIDLE, 45),
        (socket.SOL_TCP, socket.TCP_KEEPINTVL, 10),
        (socket.SOL_TCP, socket.TCP_KEEPCNT, 6)
    ]
)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72393655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档