Python,连接拒绝 10061 [英] Python, Connectin Refused 10061

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

问题描述

我一直收到这个错误

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

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

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

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.

客户端代码

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()

服务器代码

# !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()

推荐答案

猜测你的缩进,然后运行你的代码……它工作得很好.*(只要我输入 127.0.0.1 当它询问我的 IP 时.)

Taking a guess at your indentation, and running your code… it works just fine.* (As long as I type in 127.0.0.1 when it asks me for the 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.

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

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_REUSEADDR in the docs.)

*工作正常"是指它连接,并在退出前打印出以下内容:

* 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

显然它从不向服务器发送任何内容或接收任何内容,因为服务器没有 sendrecv 调用或其他任何东西.

Obviously it never sends anything to the server or receives anything back, because the server has no send or recv calls, or anything else.

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

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