首页 文章

为什么我在Python中收到错误“连接被拒绝”? (插座)

提问于
浏览
45

我是Sockets的新手,请原谅我完全缺乏理解 .

我有一个服务器脚本(server.py):

#!/usr/bin/python

import socket #import the socket module

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

s.listen(5) #Wait for the client connection
while True:
    c,addr = s.accept() #Establish a connection with the client
    print "Got connection from", addr
    c.send("Thank you for connecting!")
    c.close()

和客户端脚本(client.py):

#!/usr/bin/python 

import socket #import socket module

s = socket.socket() #create a socket object
host = '192.168.1.94' #Host i.p
port = 12397 #Reserve a port for your service

s.connect((host,port))
print s.recv(1024)
s.close

我转到桌面终端并输入以下命令启动脚本:

python server.py

之后,我去我的笔记本电脑终端并启动客户端脚本:

python client.py

但是我收到以下错误:

文件“client.py”,第9行,在s.connect((host,port))文件“/usr/lib/python2.7/socket.py”,第224行,在meth return getattr(self._sock, name)(* args)socket.error:[Errno 111]连接被拒绝

我尝试使用不同的端口号无济于事 . 但是,我能够在客户端脚本中使用相同的ip和gethostname()方法获取主机名,我可以ping桌面(服务器) .

6 回答

  • 1

    代替

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

    你应该试试

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

    这样监听套接字不会受到太多限制 . 也许否则监听仅发生在一个接口上,而接口又与本地网络无关 .

    一个例子可能是它只监听 127.0.0.1 ,这使得从不同的主机连接变得不可能 .

  • 1

    此错误意味着无论出于何种原因客户端无法连接到运行服务器脚本的计算机上的端口 . 这可能是由少数事情引起的,例如缺少到目的地的路由,但由于您可以ping服务器,情况应该不是这样 . 另一个原因可能是您的客户端和服务器之间有防火墙 - 它可能位于服务器本身或客户端上 . 鉴于您的网络寻址,我假设服务器和客户端都在同一个局域网上,因此不应该涉及任何阻止流量的路由器 . 在这种情况下,我会尝试以下方法:

    • 检查你是否真的在服务器上监听该端口(这应该告诉你你的代码是否符合你的想法):基于你的操作系统,但在linux上你可以做类似 netstat -ntulp 的事情

    • 从服务器检查,如果您正在接受与服务器的连接:再次基于您的操作系统,但 telnet LISTENING_IP LISTENING_PORT 应该完成工作

    • 检查您是否可以从客户端访问服务器的端口,但不使用代码:只需从客户端使用telnet(或适用于您的操作系统的命令)

    然后让我们知道调查结果 .

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

    我认为此错误可能与DNS解析有关 . 这句话 host = socket.gethostname() 获取主机名,但如果操作系统无法将主机名解析为本地地址,则会收到错误 . Linux操作系统可以修改 /etc/hosts 文件,在其中添加一行 . 它看起来像下面('hostname'是 socket.gethostname() 得到的) .

    127.0.0.1   hostname
    
  • 7

    在终端中尝试此命令:

    sudo ufw enable
    ufw allow 12397
    
  • 0

    假设s = socket.socket()服务器可以通过以下方法绑定:方法1:

    host = socket.gethostname()
    s.bind((host, port))
    

    方法2:

    host = socket.gethostbyname("localhost")  #Note the extra letters "by"
    s.bind((host, port))
    

    方法3:

    host = socket.gethostbyname("192.168.1.48")
    s.bind((host, port))
    

    如果您在客户端没有使用相同的方法,则会收到错误: socket.error errno 111 connection refused.

    因此,您必须在客户端使用与获取主机完全相同的方法,就像在服务器上一样 . 例如,对于客户端,您将相应地使用以下方法:

    方法1:

    host = socket.gethostname() 
    s.connect((host, port))
    

    方法2:

    host = socket.gethostbyname("localhost") # Get local machine name
    s.connect((host, port))
    

    方法3:

    host = socket.gethostbyname("192.168.1.48") # Get local machine name
    s.connect((host, port))
    

    希望能解决问题 .

  • 0

    在您的server.py文件中: host ='192.168.1.94' 而不是 host = socket.gethostname()

相关问题