Communication between 2 computers via python sockets not working

Hi, I’ve got a little of a weird problem. I have code of simple client-server communication, one of the computers as client and other as server or the opposite.
While computer 1 can establish connection with computer 2 when he acts as the client, a connection cannot be formed when he is the server. He isn’t accepting any connections, from any other computer. I’m not sure why this happens as the opposite proccess works. if it helps, the codes:

import socket
IP = ""
PORT = 8820

def main():

    server_socket = socket.socket()
    server_socket.bind(("", PORT))
    print("server on")
    (proxy_socket, proxy_address) = server_socket.accept()
    print("client on")
    while True:
        data = proxy_socket.recv(1024).decode()
        data_to_proxy = f"Hi {data}"
        if "EXIT" in data_to_proxy:


import socket
IP_PROXY = "*.*.*.*"
PORT = 8820

def main():
    # Use a breakpoint in the code line below to debug your script.
    my_socket = socket.socket()
    my_socket.connect((IP_PROXY, PORT))
    while True:
        user_input = input("Enter your message")
        data = my_socket.recv(1024).decode()
        print(f"the data is {data}")
        if user_input == "EXIT":

Thank you

Are the two computers on the same network? For example, the IP addresses and would be on the same network, but if you have and then they’re on different networks, and routing may be a factor here. (That’s an oversimplification but hopefully you know what I mean, or that’s enough to go and research it. Look up “netmask” or “CIDR” to find out more.)

It’s possible that you have a firewall. Personal firewalls often attempt to block unauthorized applications from listening on any socket; real firewalls may very well control exactly which directions connections may be established in. Do you control both computers?

Are other programs able to listen on sockets?

Your code looks pretty fine to me, aside from the fact that nothing is calling main(). Are these running as stand-alone scripts, or are they embedded inside something else?

The networks are identical, I do control both computers, and I’ve been wondering if there is some firewall blocking the connection. Should I disactivate something specific?

Hmm, depends on what sort of systems they are. Personal firewalls tend to be found on Windows systems and include Windows Defender (the one Microsoft ships) and various antivirus and general “protection” programs. However, I can’t advise in specifics since I haven’t used Windows in many many years.

On a Mac, I’m not sure what it’s called, but there’s a built-in thing that usually pops up if you try to listen on a port. You would have to research that yourself and see if it has an option to block without prompting. Again, can’t help with specifics as I don’t own a Mac, but there should be info out there.

On Linux, which I do use, firewalling is more a matter of IP traffic rules. What you’re looking for is iptables or nftables. Try commands like:

$ sudo iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     0    --            /* stat: Local input */
ACCEPT     0    --            /* stat: LAN input */
ACCEPT     0    --              /* stat: Sikorsky download */

Chain FORWARD (policy DROP)
target     prot opt source               destination         
ACCEPT     1    --             
ACCEPT     0    --         state RELATED,ESTABLISHED
ACCEPT     0    --              state RELATED,ESTABLISHED /* stat: Walled Garden via wifi upload */
ACCEPT     0    --              state RELATED,ESTABLISHED /* stat: Walled Garden via wired upload */
ACCEPT     0    --              state RELATED,ESTABLISHED /* stat: Walled Garden download */
ACCEPT     6    --           tcp dpt:22
ACCEPT     6    --           tcp dpt:80
ACCEPT     17   --         udp dpt:64738
DROP       0    --        
ACCEPT     6    --              tcp dpt:21
ACCEPT     6    --              tcp dpt:22
ACCEPT     6    --              tcp dpt:23
ACCEPT     6    --              tcp dpt:53

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     0    --              owner GID match 1003 reject-with icmp-port-unreachable
ACCEPT     0    --            /* stat: Local output */
ACCEPT     0    --         /* stat: LAN output */

(cut-down view of my own firewall; note that there’s a lot of ACCEPT lines for statistical purposes, but generally, this is the sort of info you’d get)

I’m really not familiar with the nftables commands, as they tend to be more low-level and designed for other tools rather than for human consumption, so you’d have to see if you have any sort of firewall application installed.

Note that it’s entirely possible for a firewall to be on EITHER machine, but at least with personal firewalls, it’s more likely to be the one that’s unable to act as a server.

1 Like

Thanks, I’ll try