我正在尝试将消息从一个点发送到另一个点,它已经工作了一段时间,然后当我回到家时,它无效并且我收到此错误:
line 20, in connect
self.mySocket.connect((self.serverName, self.serverPort))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
这些代码如下。我甚至尝试在Mac上运行它,我也遇到了类似的错误。
客户端
import socket
serverName = "my_IP_Address"
serverPort = 12000
class User:
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def __init__(self):
self.serverName = serverName
self.serverPort = serverPort
def connect(self):
print("Trying to connect at " + str(self.serverName) + " on port " + str(self.serverPort))
self.mySocket.connect((self.serverName, self.serverPort))
def sendMessage(self, msg):
newS = msg.encode('utf-8')
self.mySocket.send(newS)
modifiedData = self.mySocket.recv(2048)
def closeSocket(self):
self.mySocket.close()
def listen(self):
self.mySocket.bind(('', self.serverPort))
self.mySocket.listen(1)
print("We are ready to receive data on port " + str(serverPort))
while (1):
print("** attempting ** ")
connectionSocket, addr = self.mySocket.accept()
rcvMsg = connectionSocket.recv(2048)
newMsg = "I have received: " + rcvMsg.decode('utf-8')
print(newMsg)
newMsg = newMsg.encode('utf-8')
connectionSocket.send(newMsg)
connectionSocket.close()
if (rcvMsg.decode('utf-8') == 'done'):
break
print("done listening")
def main():
userA = User()
userA.listen()
main()
服务器
def main():
userA = User()
userA.connect()
msg = "Hello from the lab"
while(msg != "done"):
userA.sendMessage(msg)
msg = input("please type another message to send: ")
print("done transmitting messages")
main()
任何方向都会非常感谢!
答案 0 :(得分:1)
我发送了太多请求,我的ISP阻止了请求(因此它拒绝了连接)。一旦我搬到新网络,我就能再次使用它。