Python socket.error: [Errno 111] 连接被拒绝 [英] Python socket.error: [Errno 111] Connection refused

查看:256
本文介绍了Python socket.error: [Errno 111] 连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个使用套接字传输文件的程序.代码的服务器端运行良好.但是,在客户端我收到以下错误

I am trying to write a program for file transfer using sockets. The server end of the code is running fine. However, in the client side I get the following error

Traceback (most recent call last):
File "client.py", line 54, in <module>
uploadFiles(directory)
File "client.py", line 36, in uploadFiles
transferFile(fname)
File "client.py", line 13, in transferFile     
cs.connect((HOST, 36258))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

我的代码如下

import os
import socket

def transferFile(fname):
   HOST = '127.0.0.1'
   CPORT = 36258
   MPORT = 36250
   FILE = fname
   cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   cs.connect((HOST, 36258))
   cs.send("SEND " + FILE)
   cs.close()
   ms = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   ms.connect((HOST, MPORT))
   f = open(FILE, "rb")
   data = f.read()
   f.close()
   ms.send(data)
   ms.close()

def uploadFiles(directory):
   home = os.getenv("HOME")
   folder = str(home + "/" + directory)
   os.chdir(folder)
   dirList = os.listdir(folder)
   print dirList
   for fname in dirList:
       if fname.endswith('.bin'):
           transferFile(fname)

os.chdir(os.getenv("HOME"))
directory = "testdownload"
if not os.path.exists(directory):
   os.makedirs(directory)
 os.chdir(directory)

uploadFiles(directory)

我尝试在 Google 和 Stack Overflow 上的其他帖子上寻求帮助,但都没有帮助.有人可以帮帮我吗?行号可能不同,因为我只粘贴了部分代码

I tried looking for help on Google and other posts on Stack Overflow, none of them helped. Can someone please help me? The line numbers may be different since I pasted only some part of the code

推荐答案

问题显然是(如您所见)在您尝试连接时服务器端的端口 36250 未打开(因此连接被拒绝)).我可以看到服务器应该在另一个连接上收到 SEND 命令后打开这个套接字,但它显然没有与客户端同步打开 [它]".

The problem obviously was (as you figured it out) that port 36250 wasn't open on the server side at the time you tried to connect (hence connection refused). I can see the server was supposed to open this socket after receiving SEND command on another connection, but it apparently was "not opening [it] up in sync with the client side".

嗯,主要原因是没有任何同步.调用:

Well, the main reason would be there was no synchronisation whatsoever. Calling:

cs.send("SEND " + FILE)
cs.close()

将数据放入操作系统缓冲区;close 可能会刷新数据并推送到网络中,但它几乎肯定会在数据到达服务器之前返回.在 close 之后添加 sleep 可能会缓解这个问题,但这不是同步.

would just place the data into a OS buffer; close would probably flush the data and push into the network, but it would almost certainly return before the data would reach the server. Adding sleep after close might mitigate the problem, but this is not synchronisation.

正确的解决方案是确保服务器已打开连接.这将需要服务器向您发送一些消息(例如 OK,或更好的 PORT 36250 以指示连接位置).这将确保服务器已经在监听.

The correct solution would be to make sure the server has opened the connection. This would require server sending you some message back (for example OK, or better PORT 36250 to indicate where to connect). This would make sure the server is already listening.

另一件事是您必须检查send 的返回值,以确保从缓冲区中取出了多少字节.或者使用 sendall.

(抱歉打扰了这个迟到的答案,但我发现这是一个高流量问题,我真的不喜欢评论部分中的睡眠想法.)

(Sorry for disturbing with this late answer, but I found this to be a high traffic question and I really didn't like the sleep idea in the comments section.)

这篇关于Python socket.error: [Errno 111] 连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆