我用创建了一个简单的测试用例test.py
print('Hello)
Python 3.7.9
我有一个AWS安全组,它在端口22上将我的IP地址列入白名单。我连接到:
ssh -v -2 -L 5678:localhost:5678 -i "~/.ssh/myssh.pem" ec2-user@<external ec2 IP address>
登录后,我按如下方式启动test.py:
$ python3 -m debugpy --listen <ec2 internal IP here>:5678 --wait-for-client -m test
当我尝试从VS代码(使用F5(连接时,我得到:
debug1: Connection to port 5678 forwarding to localhost port 5678 requested.
debug1: channel 3: new [direct-tcpip]
channel 3: open failed: connect failed: Connection refused
debug1: channel 3: free: direct-tcpip: listening port 5678 for localhost port 5678, connect from 127.0.0.1 port 35636 to 127.0.0.1 port 5678, nchannels 4
我还尝试在一个更复杂的测试用例中使用这种方法,在这段代码之前有一个print语句,它会打印到控制台,这样我就知道应用程序正在正确启动。
# Allow other computers to attach to debugpy at this IP address and port.
debugpy.listen(('<my internal ec2 IP here>', 5678))
# Pause the program until a remote debugger is attached
debugpy.wait_for_client()
在VS代码中,我有这个launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/home/ec2-user/webapp"
}
]
}
]
}
关于我为什么拒绝连接的想法?
这里的问题是EC2控制台定义了EC2实例的内部IP地址,这就是我指定的。但实际需要的是localhost IP 127.0.0.1,因为ssh隧道在该地址"出现"。