【问题标题】:i got error 10061 in python我在 python 中收到错误 10061
【发布时间】:2016-10-16 11:40:12
【问题描述】:

我试图在 python 中向自己发送一条消息,但客户端代码给了我错误 10061 服务器正常工作它工作得很好,但它突然开始给我错误。我尝试更改端口,但它仍然给我同样的错误

服务器

from socket import *
from datetime import *
def main():
    s = socket()
    client_socket = None
    s.bind(("127.0.0.1",8200))
    print "1:time"
    print "2:get list of files"
    print "3:download file"
    print "4:quit"
    input()
    s.listen(1)
    client_socket, client_address = s.accept()
    strn=client_socket.recv(4096)
    if int(strn[0])>4 or int(strn[0])<1:
        print "please send a num between 1-4"
    elif int(strn[0])==1:
        print datetime.time(datetime.now())
    elif int(strn[0])==2:
        folder=raw_input("enter the name of the folder")
        dir(folder)
    client_socket.close()
    s.close()
    input()
if name == '__main__':
        main()

客户

from socket import *
def main():
    s = socket()
    s.connect(("127.0.0.1",8200))
    buf = raw_input()
    s.send(buf)
    s.close()
    input()
if name == '__main__':
    main()

错误

Traceback (most recent call last):
  File "D:\client.py", line 10, in <module>
    main()
  File "D:\client.py", line 4, in main
    s.connect(("127.0.0.1",8200))
  File "C:\Python27\lib\socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10061] No connection could be made because the target machine actively refused it

【问题讨论】:

  • 总是添加(有问题的)完整的错误信息。
  • s.connect(("Ip",8200))/s.bind(("Ip",8200)) --> 你应该输入一个实际的IP地址而不是“Ip”跨度>
  • 我知道,当我运行代码时,我使用的是真实 IP,它仅用于示例
  • 你可以在问题中输入127.0.0.1。没有理由掩盖您的本地主机 ip

标签: python sockets server


【解决方案1】:

当目标机器拒绝连接时发生10061错误。 在您的情况下,最可能的原因是 s.bind 和 s.connect 中的“IP”尝试放置实际 IP 或 127.0.0.1。它应该工作

【讨论】:

    【解决方案2】:

    没关系,我修复了它,问题是输入是在听之前,它停止了程序谢谢大家

    【讨论】:

      猜你喜欢
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      相关资源
      最近更新 更多