Why am I getting the error “connection refused” in Python? (Sockets)

后端 未结 7 1438
轻奢々
轻奢々 2020-12-01 02:48

I\'m new to Sockets, please excuse my complete lack of understanding.

I have a server script(server.py):

#!/usr/bin/python

import socket #import t         


        
7条回答
  •  醉酒成梦
    2020-12-01 03:26

    Instead of

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

    you should try

    port = 12397 # Reserve a port for your service
    s.bind(('', port)) #Bind to the port
    

    so that the listening socket isn't too restricted. Maybe otherwise the listening only occurs on one interface which, in turn, isn't related with the local network.

    One example could be that it only listens to 127.0.0.1, which makes connecting from a different host impossible.

提交回复
热议问题