简体   繁体   English

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

[英]Python socket.error: [Errno 111] Connection refused

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

My code is as follows我的代码如下

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)

I tried looking for help on Google and other posts on Stack Overflow, none of them helped.我尝试在 Google 和 Stack Overflow 上的其他帖子上寻求帮助,但都没有帮助。 Can someone please help me?有人可以帮帮我吗? The line numbers may be different since I pasted only some part of the code行号可能不同,因为我只粘贴了部分代码

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

Well, the main reason would be there was no synchronisation whatsoever.嗯,主要原因是没有任何同步。 Calling:打电话:

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

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. close可能会刷新数据并推送到网络中,但它几乎肯定会在数据到达服务器之前返回。 Adding sleep after close might mitigate the problem, but this is not synchronisation .close后添加sleep可能会缓解问题,但这不是同步

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).这将需要服务器向您发回一些消息(例如OK或更好的PORT 36250以指示连接位置)。 This would make sure the server is already listening.这将确保服务器已经在侦听。

The other thing is you must check the return values of send to make sure how many bytes was taken from your buffer.另一件事是您必须检查send的返回值以确保从您的缓冲区中取出了多少字节。 Or use sendall .或者使用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.) (很抱歉打扰了这个迟到的回答,但我发现这是一个高流量问题,我真的不喜欢评论部分的睡眠想法。)

I am also having the same problem 

 Traceback (most recent call last):
  File "sample3.py", line 7, in <module>
    practice.connect(("192.168.43.130", 22))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 101] Network is unreachable
                                                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM