0

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
asked Apr 8, 2017 at 23:43
3
  • 1
    It's client, addr = server.accept() (comma (,), not dot (.) between client and addr). Commented Apr 8, 2017 at 23:50
  • Thank you for the fast respons! Commented Apr 8, 2017 at 23:56
  • Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content under the CC BY-SA 3.0 license. By SE policy, any vandalism will be reverted. Commented Jan 24, 2018 at 3:40

1 Answer 1

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.

answered Apr 8, 2017 at 23:50
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the fast respons!
glad I could help :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.