at this siteにあるサンプルコードから単純なクライアントサーバーのpythonソケットプログラムを作成しようとしています。私が行った唯一の実質的な変更は、print()
の機能を更新することでした。私は不可解なConnectionRefusedError [WinError 10061]
を取得していますし、なぜこのような単純な例では把握することはできません。Pythonサーバーが積極的に接続を拒否します
クライアントコード:
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を実行しているIDLEから実際の結果:
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は2つの独立したIDLEウィンドウで動作します。 私はWindowsファイアウォールのすべての機能をオフにしました。
ファイアウォールはありますか? –
はい、オフになっています。元の質問のプレゼンテーションでは、ありがとうございます。 – TomServo
通常、接続のリセットは、ファイアウォールまたはOSから行われます。 Windowsだから:もう一度電源を入れてみましたか? –