I'm building a chat server for a school project, but when I ran the code I got this error:
TypeError: bind(): AF_INET address must be tuple, not str
import socket
import select
#server booting up with ip port (server.bind) and listnes for new connections (server.listen) and printing in the and booting up to let the user know
server = socket.socket()
server.bind(("192.168.1.14, 4434"))
server.listen(5)
inputs = [server]
print("booting up the server...")
#notify for new connections
def notify_all(msg, non_receptors):
for connection in inputs:
if connection not in non_receptors:
connection.send(msg)
#function that check for new connections and greets the new users updating the amount of people connected to the server
def greet(client):
names = [n.getpeername() for n in inputs if n is not client and n is not server]
greetMsg = "hello user! \n users online:" str(names)
client.send(greetMsg.encode())
while inputs:
readables, _, _ = select.select(inputs, [], [])
for i in readables:
if i is server:
client, address = server.accept()
inputs.append(client)
print("connected to new client")
greet(client)
notify_all(f"client {address} enterd".encode(), [server, client])
else:
try:
data = i.recv(1024)
notify_all(str(str(i.getpeername()) ">>>" data.decode()).encode(), [server, i])
except Exception as e:
print(e)
inputs.remove(i)
print(f"client {i.getpeername()} BYE")
CodePudding user response:
("192.168.1.14, 4434")
is exactly the same as "192.168.1.14, 4434"
(without parentheses), which is a string. As the error message says, the argument to bind
should be a tuple, not a string:
server.bind(("192.168.1.14", 4434))
CodePudding user response:
The problem line is: server.bind(("192.168.1.14, 4434"))
.
You pass it a single string of "192.168.1.14, 4434"
instead of a tuple that contains two separate values.
You need to change it to: server.bind(("192.168.1.14", 4434))
Note that the port should be int, not str.