Python Socket Not Able To Connect To Windows Server

I am using socket library in python and want to connect client (my computer) to server (AWS EC2 Windows instance). But client always give error:- "TimeoutError: [WinError 10060] connection failed because connected host has failed to respond"

Solutions that I had already implemented:-

  1. Edit security group to enable required incoming ports.
  2. Allocate static IP for EC2 instance
  3. Restart EC2
  4. Enable incoming port from windows firewall inside EC2 instance

When I go to task manager >> performance >> Resourece Monitor, I can see that port is open:-

  • NetWork Activity

Image PID Address Send (B/sec) Receive (B/sec) Total (B/sec)

pythonw.exe 1456 EC2AMAZ-XXXXXX 627 647 1,274

  • TCP Connections

Image PID Local Address Local Port Remote Address Remote Port Packet Loss (%) Latency (ms)

pythonw.exe 1456 IPv4 loopback 50770 IPv4 loopback 50769 0 0

  • Listening Ports

Image PID Address Port Protocol Firewall Status

pythonw.exe 1456 IPv4 loopback 3469 TCP Allowed, not restricted

But I am still not able to ping EC2 on required port (telnet PUBLIC_IP PORT). And hence client is not able to connect via socket.


server.py

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 3469

s.bind(('localhost', port))
print ('Socket binded to port 3469')

s.listen(1)
print ('socket is listening')





while True:

    try:

        print('waiting for connection')
        connection, client_address = s.accept()
        print ('Got connection from ', client_address)

        data = connection.recv(16)
        print('received {!r}'.format(data))

        connection.close(); s.close();break;

    except KeyboardInterrupt:
        connection.close(); s.close();break;

Client.py

import socket 


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


port = 3469


s.connect(('EC2-PUBLIC-IP-ADDRESS-OF-EC2-HERE', port))

z = 'Hello World'


s.sendall(z.encode())

s.close()

This code works perfectly fine when both server and client are on localhost.

Can't figure out what is missing. Please help.

Thanks and Regards


s.bind(('localhost', port))

You appear to only be listening on localhost which is the loopback address and not available on the public internet.