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

后端 未结 7 1381
轻奢々
轻奢々 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:28
    host = socket.gethostname()  # Get the local machine name
    port = 12397                 # Reserve a port for your service
    s.bind((host,port))          # Bind to the port
    

    I think this error may related to the DNS resolution. This sentence host = socket.gethostname() get the host name, but if the operating system can not resolve the host name to local address, you would get the error. Linux operating system can modify the /etc/hosts file, add one line in it. It looks like below( 'hostname' is which socket.gethostname() got).

    127.0.0.1   hostname
    
    0 讨论(0)
提交回复
热议问题