Home » Python » python – How do I resolve ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it-Exceptionshub

python – How do I resolve ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

When trying to run my program I keep getting this error below.

Traceback (most recent call last):
File “C:\Users\R\Desktop\CeaserCrypt\Program_B.py”, line 49, in
get_the_file()
File “C:\Users\R\Desktop\CeaserCrypt\Program_B.py”, line 13, in get_the_file
s.connect((host, port)) # Bind to the port
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

What does this mean and why am I getting it?
This is the code:

 import socket

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

    host = socket.gethostname() # Get local machine name
    port = 12345 # Reserve a port for your service.
    s.connect((host, port)) # Bind to the port

    f = open("T.dat", "w")
    data = None
    while True:
        m = s.recv(1024)
        data = m
        if m:
            while m:
                m = s.recv(1024)
                data += m
            else:
                break
    f.write(data)
    f.close()
    print("Received the file..")
    return

And yes, my firewall/antivirus is off

How to&Answers: