Socket in python

I am doing the discipline of networks and computers and I am having a problem in the following Question:

Using TCP, deploy a server that offers two features: uppercase and daytime. The customer, when sending a message to the server, should receive as a response the message in capital letters and the time the server received your request.

Well, I am not being able to use the function upper() on the server. follow below my code.

TCPServer.py

from socket import *  

serverPort = 3000

serverSocket = socket(AF_INET, SOCK_STREAM)

#atribui a porta ao socket criado
serverSocket.bind(('', serverPort))    

#aceita conexões com no máximo um cliente na fila 
serverSocket.listen(1)

print('The server is ready to receive')


while True:
    connectionSocket, addr = serverSocket.accept()         

    #recebe a mensagem do cliente em bytes
    mensagem = connectionSocket.recv(1024)     



    #envio tbm deve ser em bytes
    mensagem = mensagem.upper()
    connectionSocket.send(mensagem)

    connectionSocket.close()

TCPClient.py

from socket import *   

serverName = 'localhost'

mensagem = "gustavo"

serverPort = 3000


clientSocket = socket(AF_INET, SOCK_STREAM)

clientSocket.connect((serverName, serverPort))


#a mensagem deve estar em bytes antes de ser enviada ao buffer de transmissão
clientSocket.send(mensagem.encode())


#recebe a resposta do servidor
clientSocket.recv(1024)

#devemos converter a mensagem de volta para string antes de imprimí-la
print('Resposta:' , mensagem)

#fecha a conexão
clientSocket.close()

Well, what's going on in my code, you must be wondering! well, it connects the server normally and the client output is not uppercase.

I would like help using UPPERCASE on the TCP server.

Author: Gustavo Dazzle, 2019-09-24

1 answers

In line 20 where clientSocket.recv(1024) is, you did not pass the answer to the variable mensagem. So what was printed was the message you sent and not the one you received.

I know it's not related to your question, but don't forget to decode the string when receiving it from the recv method, otherwise you might get multiple errors in your code.

Correct client code:

from socket import *   

serverName = 'localhost'

mensagem = "gustavo"

serverPort = 3000


clientSocket = socket(AF_INET, SOCK_STREAM)

clientSocket.connect((serverName, serverPort))


#a mensagem deve estar em bytes antes de ser enviada ao buffer de transmissão
clientSocket.send(mensagem.encode())


#recebe a resposta do servidor
msg = clientSocket.recv(1024).decode()

#devemos converter a mensagem de volta para string antes de imprimí-la
print('Resposta:' , msg)

#fecha a conexão
clientSocket.close()
 1
Author: JeanExtreme002, 2020-06-11 14:45:34