python服务器主动拒绝连接
python server actively refuses connection
我正在尝试根据位于 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 -nab
-- 它显示我的 python 服务器进程监听端口 12345:
TCP 127.0.0.1:12345 0.0.0.0:0 LISTENING
[pythonw.exe]
"the target machine actively refused it" 的错误消息对我来说毫无意义,因为服务器显然正在监听。任何人都可以提出这个简单示例中可能有什么问题吗? Windows 10,Python 3.6.1 运行 在两个独立的 IDLE windows 中。 我已经关闭了Windows防火墙的所有功能。
我发现了问题。我必须在启动 IDLE 实例时选择 "Run as Administrator" 来提升我在 IDLE 运行 服务器实例上的特权 - 并且只有服务器 - 。 那还产生了结果 IDLE 输出 shell 与显示客户端输出的 shell 分开的效果。
我正在尝试根据位于 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 -nab
-- 它显示我的 python 服务器进程监听端口 12345:
TCP 127.0.0.1:12345 0.0.0.0:0 LISTENING
[pythonw.exe]
"the target machine actively refused it" 的错误消息对我来说毫无意义,因为服务器显然正在监听。任何人都可以提出这个简单示例中可能有什么问题吗? Windows 10,Python 3.6.1 运行 在两个独立的 IDLE windows 中。 我已经关闭了Windows防火墙的所有功能。
我发现了问题。我必须在启动 IDLE 实例时选择 "Run as Administrator" 来提升我在 IDLE 运行 服务器实例上的特权 - 并且只有服务器 - 。 那还产生了结果 IDLE 输出 shell 与显示客户端输出的 shell 分开的效果。