Python,连接拒绝 10061

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13773024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:34:03  来源:igfitidea点击:

Python, Connectin Refused 10061

pythonwindowssocketsnetworking

提问by cgasser

I keep getting this error

我不断收到此错误

[Errno 10061] No connection could be made because the target machine actively refused it.

[Errno 10061] 无法建立连接,因为目标机器主动拒绝它。

I'm running Windows 7 64 bit, no virus or protection software, and python is allowed through my firewall (I've also tried turning my firewall completely off but same result). When I run the server and use telnet it connects just fine. When I try to connect to the server with the client it fails. Any suggestions as to what I could try to fix this? If you need more information just ask and I'll provide.

我正在运行 Windows 7 64 位,没有病毒或保护软件,并且 python 允许通过我的防火墙(我也尝试过完全关闭我的防火墙,但结果相同)。当我运行服务器并使用 telnet 时,它连接得很好。当我尝试使用客户端连接到服务器时,它失败了。关于我可以尝试解决这个问题的任何建议?如果您需要更多信息,请询问,我会提供。

Client Code

客户代码

import socket
import sys
def main():
   host = ""
   port = 8934
   message = "Hello World!"

   host = raw_input("Enter IP: ")
   #Create Socket
   try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   except socket.error, msg:
      print "Failed to create socket. Error code: %s Error Message: %s"%(str(msg[0]),msg[1])
      sys.exit()
   print "Socket created"

   #Connec to Server
   print host
   print port
   s.connect((host,port))
   print "You are connected to %s with IP adress of %s"%(host,host)

   #Send Data
   try:
      s.sendall(message)
   except socket.error:
      print "Failed to send."
   #Receive Data
      reply = s.recv(4096)

if __name__ == "__main__":
   main()

Server Code

服务器代码

# !usr/bin/python

import socket
import sys

HOST = ""
PORT = 8934

def main():
   #Setup socket
   try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   except socket.error,msg:
      print "Unable to create socket"
      sys.exit()
   print "Socket created."

   #Bind to adress
   try:
      s.bind((HOST,PORT))
   except socket.error,msg:
      print "Bind failed. Closing..."
      sys.exit()
   print "Socket bound."

   #Start listening
   s.listen(10)
   print "Socket Listening"

   #Accept connection
   conn, addr = s.accept()
   print "Connected to %s:%s"%(addr[0],addr[1])

if __name__ == "__main__":
   main()

采纳答案by abarnert

Taking a guess at your indentation, and running your code… it works just fine.* (As long as I type in 127.0.0.1when it asks me for the IP.)

猜一下你的缩进,然后运行你的代码……它工作得很好。*(只要我在127.0.0.1它要求我输入 IP 时输入。)

Of course the second time I run the client (if I haven't restarted the server) I get a connection-refused error. But that's just because you've coded a server that immediately quits as soon as it gets the first connection. So the second time you run the client, there is no server, so the OS rejects the connection.

当然,第二次运行客户端时(如果我还没有重新启动服务器),我会收到连接被拒绝的错误。但这只是因为您编写了一个服务器,该服务器在获得第一个连接后立即退出。所以第二次运行客户端时,没有服务器,所以操作系统拒绝连接。

You can always run the server again, which lets you run the client one more time. (Except that the server may get a 10048 error when it tries to bind the socket, because the OS is keeping it around for the previous owner. If you see that, look at SO_REUSEADDRin the docs.)

您始终可以再次运行服务器,这让您可以再运行一次客户端。(除了服务器在尝试绑定套接字时可能会收到 10048 错误,因为操作系统将其保留给前任所有者。如果您看到这一点,请查看SO_REUSEADDR文档。)

* By "works just fine" I mean that it connects, and prints out the following before quitting:

*“工作正常”我的意思是它连接,并在退出之前打印出以下内容:

Socket created
127.0.0.1
8934
You are connected to 127.0.0.1 with IP adress of 127.0.0.1

Obviously it never sends anything to the server or receives anything back, because the server has no sendor recvcalls, or anything else.

显然它永远不会向服务器发送任何东西或接收任何东西,因为服务器没有sendrecv调用,或其他任何东西。