I currently have this code
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = socket.gethostbyname(socket.gethostname())
port = 1111
address=(ip,port)
server.bind(address)
server.listen(1)
print("Started listening on", ip, ":", port)
client.addr=server.accept()
while True:
data = client.recv(1024)
print("received",data, "from the client")
print("Processing data")
if(data=="Hello server"):
client.send("hello client")
print("Processing done")
elif(data=="disconnect"):
client.send("goodbye")
client.close()
break
else:
client.send("Invalid data")
print("invalid data")
However i get this error message: NameError: name 'client' is not defined. But why?
iBug
37.6k8 gold badges101 silver badges155 bronze badges
1 Answer 1
Well, that is devoted to the fact that the function server.accept() return two values, the socket itself and the address. Therefore being accepted this way:
client, addr = server.accept()
would allow what you are trying to achieve.
Sign up to request clarification or add additional context in comments.
2 Comments
Eric mansen
Thank you for the fast respons!
Gal Niv
glad I could help :)
lang-py
client, addr = server.accept()(comma (,), not dot (.) betweenclientandaddr).