代码之家  ›  专栏  ›  技术社区  ›  TomServo

python服务器主动拒绝连接

  •  0
  • TomServo  · 技术社区  · 7 年前

    我正试图从下面的示例代码中创建一个简单的客户机-服务器python套接字程序 at this site . 我所做的唯一实质性改变就是更新 print() ConnectionRefusedError [WinError 10061] 在这样一个简单的例子中,我无法解释为什么:

    客户代码:

    import socket
    import sys
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('127.0.0.1', 12345)
    print('client connecting to %s port %s' % server_address)
    sock.connect(server_address)
    try:
        message = 'This is the message.  It will be repeated.'
        print('sending "%s"' % message)
        sock.sendall(message.encode('utf-8'))
        amount_received = 0
        amount_expected = len(message.encode('utf-8'))
        while amount_received < amount_expected:
            data = sock.recv(16)
            amount_received += len(data)
            print('received "%s"' % data)
    finally:
        print('closing socket')
        sock.close()
    

    服务器代码:

    import socket
    import sys
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('127.0.0.1', 12345)
    print ('server starting up on %s port %s' % server_address)
    sock.bind(server_address)
    sock.listen(1)
    while True:
        print('waiting for a connection')
        connection, client_address = sock.accept()
        try:
            print('connection from', client_address)
            while True:
                data = connection.recv(16)
                print('received "%s"' % data)
                if data:
                    print('sending data back to the client')
                    connection.sendall(data)
                else:
                    print('no more data from', client_address)
                    break
        finally:
            connection.close()
    

    预期结果:

    (SERVER should show)
    starting up on localhost port 12345
    waiting for a connection
    connection from ('127.0.0.1', *someaddress*)
    received "This is the mess"
    sending data back to the client
    received "age.  It will be"
    sending data back to the client
    received " repeated."
    sending data back to the client
    received ""
    no more data from ('127.0.0.1', *someaddress*)
    waiting for a connection
    (CLIENT should show)
    connecting to localhost port 12345
    sending "This is the message.  It will be repeated."
    received "This is the mess"
    received "age.  It will be"
    received " repeated."
    closing socket
    

    实际结果

    Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit 
    (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> 
    ================ RESTART: C:\Users\____\Desktop\test01.py ================
    server starting up on 127.0.0.1 port 12345
    waiting for a connection
    
    ================ RESTART: C:/Users/____/Desktop/test02.py ==============
    client connecting to 127.0.0.1 port 12345
    Traceback (most recent call last):
      File "C:/Users/_____/Desktop/test02.py", line 6, in <module>
        sock.connect(server_address)
    ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
    

    netstat状态

    我很好奇,看看进程是否在监听我的权限并运行 netstat -nab --它显示了我在端口12345上侦听的python服务器进程:

    TCP    127.0.0.1:12345        0.0.0.0:0              LISTENING
    [pythonw.exe]
    

    “目标机器主动拒绝它”的错误消息对我来说毫无意义,因为服务器显然正在侦听。有人能指出这个简单的例子中可能出了什么问题吗?Windows 10、Python 3.6.1在两个单独的空闲窗口中运行。

    1 回复  |  直到 7 年前
        1
  •  1
  •   TomServo    7 年前

    我发现了问题。在空闲运行服务器的实例上,我必须通过选择 启动该空闲实例时“以管理员身份运行”。 这也产生了这样的效果,即生成的空闲输出shell与显示客户端输出的shell分离。