Error using socket.recv ()

1

I'm learning about servers in python and I'm using the socket library. But when I use the socket.recv(1024) command for the server to read what the client sent, IDLE gives the following error:

  

'socket' object has no attribute 'recv'.

What could it be?

The code:

import socket

cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = "localhost"
porta = 5000

confirma = cliente.connect((host, porta))

if (confirma) == -1:
    print("ACESSO NEGADO")

else:
    print("ACESSO PERMITIDO")
    while True:
        pergunta = cliente.recv(1024)
        print(pergunta)
    
asked by anonymous 05.01.2018 / 18:18

1 answer

1

Your socket client code worked normally, it could be a problem on the server socket

Server socket:

from socket import *

s = socket(AF_INET, SOCK_STREAM)
s.bind(( "localhost", 5000 ))
s.listen(20)

try:
   conn, addr = s.accept()

   conectado = "Conectado ao server!".encode()

   conn.sendall(conectado)

   client = str(addr)
   print("Conectado: {}".format(client) )

   msg = str(conn.recv(4096))
   print("Mensagem: {}".format(msg) )
except:
   conn.close()
   s.close()

I tested with your client socket, it worked perfectly!

.

I do not know if I can help you, but I hope so!

    
26.07.2018 / 23:50