Flask端点始终返回500

时间:2019-05-15 21:29:06

标签: 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['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>

即使处理并记录了AuthenticationException,我仍然得到500 - Internal Server Error

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

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

0 个答案:

没有答案