Eric mansen
Eric mansen

Reputation: 41

Sockets python client

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?

Upvotes: 0

Views: 410

Answers (1)

Gal Niv
Gal Niv

Reputation: 68

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.

Upvotes: 1

Related Questions