Question:
I am trying to run a simple Python TCP server on my EC2, listening on port 6666. I’ve created an inbound TCP firewall rule to open port 6666, and there are no restrictions on outgoing ports.
I cannot connect to my instance from the outside world however, testing with telnet or netcat can never make the connection. Things do work if I make a connection from localhost however.
Any ideas as to what could be wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/usr/bin/env python import socket TCP_IP = '127.0.0.1' TCP_PORT = 6666 BUFFER_SIZE = 20 # Normally 1024, but we want fast response s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TCP_IP, TCP_PORT)) s.listen(1) conn, addr = s.accept() print 'Connection address:', addr while 1: data = conn.recv(BUFFER_SIZE) if not data: break print "received data:", data conn.send(data) # echo conn.close() |
Answer:
Your TCP_IP is only listening locally because you set your listening IP to 127.0.0.1.
Set TCP_IP = "0.0.0.0"
and it will listen on “all” interfaces including your externally-facing IP.