python服务器主动拒绝连接

时间:2017-10-24 20:49:50

标签: python sockets python-idle

我正在尝试从位于at this site的示例代码创建一个简单的客户端 - 服务器python套接字程序。我所做的唯一实质性更改是更新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
IDLE运行Python 3.6.1的

实际结果

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 - 它显示我的python服务器进程侦听端口12345:

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

“目标计算机主动拒绝它”的错误消息对我没有意义,因为服务器显然正在侦听。任何人都可以在这个简单的例子中暗示可能出现的问题Windows 10,Python 3.6.1在两个独立的IDLE窗口中运行。 我已关闭Windows防火墙的所有功能。

1 个答案:

答案 0 :(得分:1)

我发现了问题。我必须通过选择&#34;以管理员身份运行&#34;来提升我在运行服务器的IDLE实例(仅限服务器)上的权限。当启动那个IDLE实例时。这也导致生成的IDLE输出shell与显示客户端输出的shell分开。