简体   繁体   中英

Cannot connect to my AWS RDS instance from my local computer using python socket

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM