Return HD size and free space to client-server

Asked

Viewed 225 times

0

I would like to return to the customer the information of HD size 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'

  • Dude, your codes are all inline, being impossible to analyze anything. Could [Dit] the question and fix it?

1 answer

1


There’s a problem here:

lista = pickle.loads(dest)

dest is a tuple, so there’s no way you can decode using pickle. I imagine you want to receive data from the socket using recv or recvfrom first, and then decode.

Another mistake:

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

The print function returns nothing, so this line is putting None in resposta. Try it this way:

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

If you continue giving problem, edit the question puts the complete error including with traceback.

NOTE: I would suggest that you do not use pure sockets and pickle to transmit data in that way. There are many options, but this choice of tools has great chance to bring problems and difficulties in 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, beyond the code becoming clearer and readable, will facilitate things you may need in the future, such as cancellation and parallelism.

  • I made the switch to udp.recvfrom(msg.encode('ascii'), dest) and now I’ve got the information TypeError: 'bytes' object cannot be interpreted as an integer. In addition to the course of modifying the server code.

  • but according to the documentation here , recvfrom receives an integer as a parameter! Where did you get the idea to pass the message as a parameter? You have to look at the functions how they work, what parameters they receive and what they will return, before using them, don’t just go out putting everything in the code and hope it works

Browser other questions tagged

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