Return HD size and free space for client-server

0

I would like to return to the client HD size information beyond the available space.

Client Code

import socket, pickle
HOST = 'localhost'
PORT = 9991
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dest = (HOST, PORT)
msg = input('Entre com a mensagem:\n')
udp.sendto(msg.encode('ascii'), dest)
lista = pickle.loads(dest)
print(lista)
udp.close() 

Server Code

import socket, psutil, pickle
HOST = 'localhost'
PORT = 9991
udp = socket.socket(socket.AF.INET, socket.SOCK_DGRAM)
org = (HOST, PORT)
udp.bind(org)
print('Esperando receber na porta: ', PORT,'...')
(msg, cliente) = udp.recvfrom(1024)
if msg.decode('ascii') == 'disponivel':
    total = round(psutil.disk_usage('/').total/(1024*1024*1024),1))
    totalDisp = round(psutil.disk_usage('/').free/(1024*1024*1024),1))
    resposta = print('Memoria total e: ', total, 'e a disponivel: ', totalDisp)
    tup_resp = pickle.dumps(resposta)
    msg.send(tup_resp)
else:
    print('Argumento invalido')
udp.close()

I get the message a byte-type object is required, not 'tuple'

    
asked by anonymous 04.09.2018 / 09:23

1 answer

1

There is a problem here:

lista = pickle.loads(dest)

dest is a tuple, so you can not decode using pickle . I imagine you want to get data from the socket using recv or recvfrom first, then decode.

Another error:

resposta = print('Memoria total e: ', total, 'e a disponivel: ', totalDisp)

The print function does not return anything, so this line is setting None to resposta . Try this:

resposta = 'Memoria total e: {} e a disponivel: {}'.format(total, totalDisp)
print(resposta)

If you keep giving problem, edit the question put the complete error even with traceback.

NOTE: I would suggest not using pure sockets and pickle to transmit data in this way. There are many options, but this choice of tools has a great chance of bringing problems and difficulties into the future. Instead of pickle , use a well-defined format with future compatibility, such as json or xml . Instead of using pure sockets, use an asynchronous framework like trio , and the code becomes clearer and more readable, it will facilitate things you may need in the future, such as cancellation and parallelism.

    
04.09.2018 / 16:39