66

我是 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((主机,端口))

文件“/usr/lib/python2.7/socket.py”,第 224 行,在 meth

返回 getattr(self._sock,name)(*args)

socket.error: [Errno 111] 连接被拒绝

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

4

9 回答 9

82

代替

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,这使得从不同的主机连接是不可能的。

于 2013-04-21T11:53:36.257 回答
11

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

  • 检查你是否真的有那个端口在服务器上监听(这应该告诉你你的代码是否做了你认为应该做的事情):根据你的操作系统,但在 linux 上你可以做类似的事情netstat -ntulp
  • 如果您接受与服务器的连接,请从服务器检查:再次基于您的操作系统,但telnet LISTENING_IP LISTENING_PORT应该完成这项工作
  • 检查您是否可以从客户端访问服务器的端口,但不能使用代码:只需从客户端使用 telnet(或适用于您的操作系统的命令)

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

于 2013-04-21T11:57:04.277 回答
3

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

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

方法二:

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

方法三:

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

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

因此,您必须在客户端使用与在服务器上完全相同的方法来获取主机。例如,在客户端的情况下,您将相应地使用以下方法:

方法一:

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

方法二:

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

方法三:

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

希望能解决问题。

于 2017-09-06T09:14:26.263 回答
1

注意更改端口号。有时,您只需要更改端口号。当我对语法和功能进行更改时,我体验到了这一点。

于 2021-04-19T12:25:57.447 回答
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

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

127.0.0.1   hostname
于 2015-10-19T08:21:44.633 回答
0

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

于 2017-12-11T12:42:35.660 回答
0

我能够 ping 我的连接,但仍然收到“连接被拒绝”错误。原来我在ping自己!这就是问题所在。

于 2019-05-16T06:33:56.590 回答
0

我在我的代码中遇到了同样的问题,经过几天的搜索,我终于找到了解决方案,问题是函数 socket.gethostbyname(socket.gethostname) 在 linux 中不起作用,所以你必须使用 socket。 gethostbyname('手动输入主机名') 不是 socket.gethostbyname('localhost'),使用 socket.gethostbyname('host') 与 ifconfig 一起查看。

于 2021-06-08T01:19:15.557 回答
-1

在终端尝试这个命令:

sudo ufw enable
ufw allow 12397
于 2018-01-07T18:05:34.020 回答