Server.py(运行在我aws ec2实例上)
import socket
s = socket.socket()
host = socket.gethostbyaddr('aws.ec2.public.ip')[0]
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print('Got connection from', addr)
c.send('Thank you for connecting'.encode())
c.close()
客户端.py(在我的本地电脑上运行)
import socket
s = socket.socket()
host = socket.gethostbyaddr('aws.ec2.public.ip')[0]
port = 12345
s.connect((host, port))
print(s.recv(1024).decode())
s.close()
所有入站和出站的TCP流量均被授权
服务器代码没有显示错误。 但是客户端代码显示:
Traceback (most recent call last):
File "/Users/sohamjain/Desktop/client.py", line 7, in <module>
s.connect((host, port))
TimeoutError: [Errno 60] Operation timed out
>>>
通过rdp客户端连接EC2实例完美运行
当我在本地主机上运行这两个脚本时,它们似乎都正常工作。 但是,在AWS EC2实例的情况下,它并没有正常工作。 我哪里错了?
server.py
实例不会意识到它具有公共地址。它只会理解它具有私有IP地址,因为Internet网关在接收流量时执行NAT转换入站以将公共IP映射到私有IP。可以通过运行ifconfig
来确认这一点。 - Chris Williams