Error using socket.recv()

Asked

Viewed 355 times

1

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

'socket' Object has no attribute 'recv'.

What could 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)

1 answer

2


Your code from the socket client, worked normally, could be problems in socket server

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 did the test with your client socket, it worked perfectly!

.

I don’t know if I can help you, but I hope so!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.