无法使用 python 套接字从本地计算机连接到我的 AWS RDS 实例
当从我的计算机连接到同一网络上的另一台计算机时,此代码有效。
但是,当将服务器代码传输到具有公共IP地址的AWS实例时, 服务器代码不起作用。
我也设置了私有 IPv4 地址,但不起作用:
import socket
HOST = "Client IP"
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
s.close()
客户端代码:
import socket
HOST = "Server IP"
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
s.close()
This code works when connecting from my computer to another computer on the same network.
However, when transferring the server code to the AWS instance with a public IP address, the
server code doesn't work.
I put a private IPv4 address also, it doesn't work:
import socket
HOST = "Client IP"
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
s.close()
Client code:
import socket
HOST = "Server IP"
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
s.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过在路由器上为客户端计算机打开一个端口解决了这个问题
我还将服务器代码中的HOST变量中的IP设置为0.0.0.0
并且客户端代码中的IP是由服务器的公共IP设置的
I solved the problem by opening a port on the router for the client computer
I also put the IP in the HOST variable in the server code is 0.0.0.0
and the IP in the client code was set by the public IP of the server