Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Python TCP server not working on Amazon EC2 instance

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?



    #!/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()


like image 907
user43995 Avatar asked Jan 10 '23 01:01

user43995


1 Answers

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.

like image 50
Max Worg Avatar answered Jan 20 '23 12:01

Max Worg