1

I am trying to create a simple web server with python using the following code. However, When I run this code, I face this error:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

It worths mentioning that I have already tried some solutions suggesting manipulation of proxy settings in internet options. I have run the code both in the unticked and the confirmed situation of the proxy server and yet cannot resolve the issue. Could you please guide me through this ?

import sys
import socketserver
import socket

hostname = socket.gethostname()
print("This is the host name:  " + hostname)

port_number = 60000

soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect((hostname,port_number))
HoOman
  • 455
  • 1
  • 7
  • 15
  • Guide you through how? The port is closed to connections, maybe through a router or on the host PC – roganjosh Feb 10 '19 at 14:22
  • check the firewall – Druckermann Feb 10 '19 at 14:28
  • Do you have a socket server waiting on the port 60000? – Martin Feb 10 '19 at 15:12
  • Thank you DanielSiegel, roganjosh and @Martin Well, I don't know how to open the connection. The windows firewall is disabled. and I have not created any clients yet. and regarding Martin's question, I do not know whether I have another socket server on this port but the result is the same when I change the port number – HoOman Feb 10 '19 at 15:42
  • The only sitaution in which it does not give error is when the port_number is set to 80. Why? – HoOman Feb 10 '19 at 15:46
  • @HoOman check my answer. This should work. To connect to socket, you need to create server or have a server with corresponding protocol – Martin Feb 10 '19 at 16:03
  • @HoOman maybe because there is some socket server – Martin Feb 10 '19 at 16:05
  • @Martin Sorry I couldn't get what you mean. I just wrote the above code to create a server. Should I have done anything else previously? – HoOman Feb 10 '19 at 17:01
  • @HoOman no, thats no server but client, You dont have server yet. Check my answer. I combined your client and created server. – Martin Feb 10 '19 at 17:38

1 Answers1

0

Standard EXAMPLE of socket connection

SERVER & CLIENT

run this in your IDLE

import time
import socket
import threading
HOST = 'localhost'  # Standard loopback interface address (localhost)
PORT = 60000       # Port to listen on (non-privileged ports are > 1023)

def server(HOST,PORT):
    s =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)

    while True:
        conn, addr = s.accept()
        data = conn.recv(1024)
        if data:
            print(data)
            data = None
        time.sleep(1)
        print('Listening...')


def client(HOST,PORT,message):            
    print("This is the server's hostname:  " + HOST)


    soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    soc.connect((HOST,PORT))
    soc.send(message)
    soc.close()

th=threading.Thread(target = server,args = (HOST,PORT))
th.daemon = True
th.start()

After running this, in your IDLE execute this command and see response

>>> client(HOST,PORT,'Hello server, client sending greetings')
This is the server's hostname:  localhost
Hello server, client sending greetings
>>> 

If you try to do server with port 60000 but send message on different port, you will receive the same error as in your OP. That shows, that on that port is no server listening to connections

Martin
  • 3,333
  • 2
  • 18
  • 39