FlandRem 发表于 2022-3-17 12:56:13

WinError 10061 由于目标计算机积极拒绝,无法连接

我在尝试用Python做一个聊天室的时候 发现我的电脑上一跑就是ConnectionRefusedError: 由于目标计算机积极拒绝,无法连接。
用的
HOST = '127.0.0.1'
PORT = 13502


所以我尝试从网上下了别人的代码   发现也会报这个错
想知道这是我设置不对的原因还是什么其他因素导致的。 有什么办法解决嘛?


已经尝试过internet 属性 的局域网设置自动检测没有解决。

FlandRem 发表于 2022-3-17 13:00:44

project-main code:
server.pyimport socket
import csv
from User import User
import pickle

HOST = '127.0.0.1'
PORT = 18737      



def checkFunction(conn, data):
    #decode byte data and split the string into parts
    data = data.decode('utf-8')
    command = data.split()
    if command == "newuser":
      #call funct to check if user already exists
      result = checkUserExists(command)
      if result == True:
            #if user does not exist call funct to create the new user
            newUser(conn,command, command)
      elif result == False:
            error = "Denied. User account already exists."
            conn.send(bytes(error,'utf-8'))
    elif command == "login":
      #call login funct
      result = login(conn, command, command)
      if result == True:
            updateUserState(command, True)
            #get User obj and put in pickle to send to client
            logged = getUser(command)
            logged = pickle.dumps(logged)
            #header string to indicate login success to client
            header = bytes("login",'utf-8')
            #send header and userObj pickle to client
            conn.send(header + logged)
      elif result == False:
            conn.send(bytes("Denied. Username or password incorrect",'utf-8'))
    elif command == "send":
      #split send command into 3 parts (2 spaces) (send,username,message content)
      command = data.split(" ", 2)
      #msg create and print to server
      msg = command + ": " + command
      print(msg)
      #send msg to client
      conn.send(bytes(msg,'utf-8'))
    elif command == "logout":
      #make sure user can be logged out
      updateUserState(command, True)
      result = getUserState(command)
      if result == True:
            #make user logged in state false
            updateUserState(command, False)
            #msg and print to server
            msg = command + " logout"
            print(msg)
            #send logout msg to client
            conn.send(bytes(command + " left",'utf-8'))
      elif result == False:
            error = "Denied. Please login first"
    elif command == "retry":
      #response for if no user is logged in
      conn.send(bytes("retrying",'utf-8'))


def checkUserExists(user):
    #iterate over users in array to check if the user exists
    for name in users:
      username = name.username
      if username == user:
            #user already exists
            return False
   
    return True
      

#func to put a new user into the users.txt file
def newUser(conn,username,passw):
    #open the file and format the new user entry
    File = open("users.txt", "a")
    entry = "\n(" + username + ", " + passw + ")"
    #add the new entry to the User list
    users.append(User(username, False))
    #add the entry on a newline to the file
    File.write(entry)
    File.close()
    #report success
    success = "New user account created. Please login."
    print("New user account created.")
    conn.send(bytes(success,'utf-8'))


#func to login a user
def login(conn,user,passw):
    #open the users file
    File = open("users.txt", "r")
    #iterate over users in the file, use csv to access parts of the entries
    reader = csv.reader(File, delimiter=',')
    for row in reader:
      #get a username entry
      username = row
      #remove parenthesis
      username = username
      #compare user entered username with username in file
      if username == user:
            #if matched, get the password for the user
            recordPass = row
            #remove parenthesis
            recordPass = recordPass[:-1]
            #remove leading space
            recordPass = recordPass
            #compare passwords
            if recordPass == passw:
                print(username, "login.")
                return True
    File.close()
    return False


#func to change user logged in status
def updateUserState(user, state):
    for name in users:
      username = name.username
      if username == user:
            name.logStatus = state


#func to get logged in state of user
def getUserState(user):
    for name in users:
      username = name.username
      if username == user:
            return name.logStatus


#get a specific User obj
def getUser(user):
    for name in users:
      username = name.username
      if username == user:
            return name


#create the User obj list
def usersInit():
    users = []
    #open users file
    File = open("users.txt", "r")
    reader = csv.reader(File, delimiter=',')
    #read in usernames
    for row in reader:
      username = row
      username = username
      #create new Users with the usernames in the file
      users.append(User(username,False))
    File.close()
    return users


print("My chat room server. Version one.")

while True:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.bind((HOST, PORT))
      s.listen()
      conn, addr = s.accept()
      with conn:
            data = conn.recv(1024)
            #initiate a list of all users
            users = usersInit()
            #send data from client to function to figure out what to do
            checkFunction(conn,data)
            conn.sendall(data)
client.py:
import socket
import pickle
from User import User


HOST = '127.0.0.1'
PORT = 18737      # The port used by the server

print("My chat room client. Version One.\n")

userLogged = User("init",False)

while True:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((HOST, PORT))
      #get user input
      command = input()
      #split user input into parts on spaces
      commandParts = command.split()

      #if statement to check for login, logout, and send commands
      if commandParts == "login":
            #check for proper command usage
            if len(commandParts) == 3:
                #send login command to server
                s.send(bytes(command,'utf-8'))
                #receive response from server
               
                response = s.recv(1024)
                #decode header bytes
                result = response[:5]
                result = result.decode('utf-8')
                if result == "login":
                  #if header bytes match, get the user pickle
                  userObj = response
                  #unpickle User
                  userLogged = pickle.loads(userObj)
                  print("login confirmed")
                else:
                  #header bytes dont match, print error response
                  print(response.decode('utf-8'))
               
            else:
                print("Command usage: login <user> <pass>")
                s.send(bytes("retry",'utf-8'))
                response = s.recv(1024)
                response = response.decode('utf-8')
      elif commandParts == "logout":
            #check for proper command usage
            if len(commandParts) == 1:
            #make sure a user is logged in
                if userLogged and userLogged.logStatus == True:
                #use User object to tell the server which user to logout
                  msg = commandParts + " " + userLogged.username
                  s.send(bytes(msg,'utf-8'))
                  response = s.recv(1024)
                  #get logout response and close socket connection
                  response = response.decode('utf-8')
                  print(response)
                  s.close
                  break
                else:
                  #handle if no user is logged in
                  print("Denied. Please login first.")
                  command = "retry"
                  s.send(bytes(command,'utf-8'))
                  response = s.recv(1024)
                  response = response.decode('utf-8')
            else:
                print("Command usage: logout")
                command = "retry"
                s.send(bytes(command,'utf-8'))
                response = s.recv(1024)
                response = response.decode('utf-8')
      elif commandParts == "send":
            #make sure a user is logged in
            if userLogged and userLogged.logStatus == True:
                #split the send command to keep the message content together
                sendMsg = command.split(" ", 1)
                #check for proper send usage
                if len(sendMsg) == 2 and sendMsg == "send":
                  #insert the username into the command to send to the server
                  sendMsg.insert(1,userLogged.username)
                  sendStr = ""
                  #put the send command together and send to server
                  for ele in sendMsg:
                        sendStr += ele + " "
                  s.send(bytes(sendStr,'utf-8'))
                  response = s.recv(1024)
                  response = response.decode('utf-8')
                  print(response)
                else:
                  print("Command usage: send <message>")
                  command = "retry"
                  s.send(bytes(command,'utf-8'))
                  response = s.recv(1024)
                  response = response.decode('utf-8')
            else:
                #handle if no user is logged in
                print("Denied. Please login first.")
                command = "retry"
                s.send(bytes(command,'utf-8'))
                response = s.recv(1024)
                response = response.decode('utf-8')
      elif commandParts == "newuser":
            #check for proper newuser usage
            if len(commandParts) == 3:
                s.send(bytes(command,'utf-8'))
                response = s.recv(1024)
                response = response.decode('utf-8')
                print(response)
            else:
                print("Command usage: newuser <username> <password>")
                command = "retry"
                s.send(bytes(command,'utf-8'))
                response = s.recv(1024)
                response = response.decode('utf-8')
      else:
            #catch invalid commands
            print("Invalid Command. Potential commands are:")
            if userLogged.logStatus == True:
                print("send <message>\nlogout")
            elif userLogged.logStatus == False:
                print("newuser <username> <password>\nlogin <username> <password>")
            command = "retry"
            s.send(bytes(command,'utf-8'))
            response = s.recv(1024)
            response = response.decode('utf-8')
            
      
User.py:
class User:
    def __init__(self,username,logStatus):
      self.username = username
      self.logStatus = logStatus

isdkz 发表于 2022-3-17 13:03:39

请先执行 server.py 再执行 client.py

FlandRem 发表于 2022-3-17 13:07:39

isdkz 发表于 2022-3-17 13:03
请先执行 server.py 再执行 client.py

我就是这样操作的但还是会报错

=========== RESTART: D:\学习\CS 4850\online code\CS4850-Project-main\server.py ===========
My chat room server. Version one.

=========== RESTART: D:\学习\CS 4850\online code\CS4850-Project-main\client.py ===========
My chat room client. Version One.

Traceback (most recent call last):
File "D:\学习\CS 4850\online code\CS4850-Project-main\client.py", line 22, in <module>
    s.connect((HOST, PORT))
ConnectionRefusedError: 由于目标计算机积极拒绝,无法连接。

isdkz 发表于 2022-3-17 13:09:35

本帖最后由 isdkz 于 2022-3-17 13:11 编辑

FlandRem 发表于 2022-3-17 13:07
我就是这样操作的但还是会报错

=========== RESTART: D:\学习\CS 4850\online code\CS4850-Project-ma ...

你是在同一台电脑执行的吗?不要把 server.py 关掉,建议不要用 idle,

你打开你的源代码所在的文件夹,然后在地址栏输入 cmd,

这样执行(在两个不同的cmd下):
python server.py

python client.py

FlandRem 发表于 2022-3-17 13:19:09

isdkz 发表于 2022-3-17 13:09
你是在同一台电脑执行的吗?不要把 server.py 关掉,建议不要用 idle,

你打开你的源代码所在的文件 ...

还是会报错{:10_266:}

isdkz 发表于 2022-3-17 13:21:09

FlandRem 发表于 2022-3-17 13:19
还是会报错

你的代码中 HOST 和 PORT 分别是什么?

FlandRem 发表于 2022-3-17 13:22:34

是HOST = '127.0.0.1'
PORT = 13502

FlandRem 发表于 2022-3-17 14:11:15

isdkz 发表于 2022-3-17 13:21
你的代码中 HOST 和 PORT 分别是什么?

HOST = '127.0.0.1'
PORT = 13502

isdkz 发表于 2022-3-17 14:12:27

FlandRem 发表于 2022-3-17 14:11


client 的跟 server 的一样吗?

FlandRem 发表于 2022-3-17 14:13:26

isdkz 发表于 2022-3-17 14:12
client 的跟 server 的一样吗?

对的

isdkz 发表于 2022-3-17 14:19:19

FlandRem 发表于 2022-3-17 14:13
对的

我知道你哪里错了,你那个目标计算机拒绝确实是因为你的服务端关掉了,

我试了一下如果你的客户端什么都没有输入就按回车的话就会导致服务端崩掉,

你客户端随便输点东西再回车

FlandRem 发表于 2022-3-17 14:23:18

isdkz 发表于 2022-3-17 14:19
我知道你哪里错了,你那个目标计算机拒绝确实是因为你的服务端关掉了,

我试了一下如果你的客户端什么 ...

谢谢大佬   终于解决了
页: [1]
查看完整版本: WinError 10061 由于目标计算机积极拒绝,无法连接