Python Flask端点始终返回500的响应

Python Flask端点始终返回500的响应,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,我有以下端点,它通过ssh连接到服务器并将一些文件复制到服务器上 @upgrade_api.route("/take_copy_postgres_backup", methods=['POST']) def take_copy_postgres_backup(): try: if request.method == 'POST': data = request.get_json() hostname = data['hos

我有以下端点,它通过ssh连接到服务器并将一些文件复制到服务器上

@upgrade_api.route("/take_copy_postgres_backup", methods=['POST'])
def take_copy_postgres_backup():
    try:
        if request.method == 'POST':
            data = request.get_json()

            hostname = data['hostname']
            username = data['username']
            password = data['password']
            scp_path = data['scp_path']
            ssh_port = data['ssh_port'] or 22

            ssh = SSHClient()
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(hostname=hostname, port=ssh_port, username=username, password=password, compress=True)

        // Open a file and copy to server over scp

        return Response('Success', 200)
    except ssh_exception.BadHostKeyException as e:
        logger.error(e)
        return Response('Cannot verify SSH server host key', 404)
    except ssh_exception.AuthenticationException as e:
        logger.error(e)
        return Response(response='Authentication for SSH server failed', status=401)
    except ssh_exception.SSHException as e:
        logger.error(e)
        return Response('SSH to the server failed for some reason', 500)
    except Exception as e:
        logger.error(e)
        return Response('Internal Server Error', 500)
    finally:

        //Close the file
在提供无效的用户名和/或密码时,我希望将处理
AuthenticationException
,并返回带有401状态代码的响应。然而,在使用curl或postman到达端点时,我总是得到以下响应:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

500内部服务器错误
内部服务器错误
服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中存在错误

即使处理并记录了
AuthenticationException
,我仍然会得到
500-内部服务器错误

我尝试在最后删除泛型异常并再次运行,但仍然得到相同的响应。我还尝试运行调试器,但似乎找不到响应的实际发送位置


任何帮助都将不胜感激。谢谢。

您的最后一个块是停止返回,然后不返回,这将导致500。删除您的最后一个块或在其中加入一些逻辑。@nathan.medz是的,谢谢您的帮助。我不知道我在想什么。你能写一个答案让我接受吗?你的最后一个障碍是停止返回,然后不返回,这会导致500。删除您的最后一个块或在其中加入一些逻辑。@nathan.medz是的,谢谢您的帮助。我不知道我在想什么。你能写个答案让我接受吗?