Julien Luebbers
Julien Luebbers

Reputation: 69

python socket Windows 10 connection times out

We recently upgraded a windows computer to Windows 10 from Windows 7, doing a completely clean installation of the OS. When it ran Windows 7, we hosted a couple of simple python socket servers so that another Linux computer on the network could interface with certain drivers on the Windows machine. This system (of binding a socket server to an open port and listening for a response) worked perfectly for the year leading up to this upgrade.

When we updated the OS on the windows machine, the same python scripts that previously set up a SOCK_STREAM server connection between the two computers stopped functioning. When asked to connect, the connection simply times out.

Does anyone know any changes that might have impacted socket's ability to listen on Windows 10, and/or any fixes to this issue? In a lot of digging we have been able to turn up no apparent fixes.

Here is a basic example of the code being ran on each computer.

Windows 10:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("XX.XX.XX.XX", XXXXX))
s.listen()
while True:
    conn, addr = s.accept() 
    try:
        conn.settimeout(3)
        data = conn.recv(1024)
    except:
        break
    if not data:break
# more code follows to process the data, etc.

On Linux:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10) # this timeout can change, and it just hangs indefinitely when removed
s.connect(("XX.XX.XX.XX",XXXXX))

The IP and Port referenced above as a series of X's are available and do not throw any bugs.

Upvotes: 2

Views: 6447

Answers (1)

MilkyWay90
MilkyWay90

Reputation: 2083

This is a firewall issue (like @yorodm said), so it is not the Python code that is making the mistake, it's the computer.

I can't give a solution for the Linux computer (I assume that it is running correctly, with the firewall allowing incoming connections), but I can for the Windows 10 computer.

So here is a solution:

Go to Start, and start Cortana. Type in "Allow an app through Windows Firewall", then tap the "Enter" key. Click on the "change settings" button, and next to "python.exe", check the box to the left, and if it isn't already enabled, the two to the right of python.exe.

Upvotes: 3

Related Questions