Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Send a def with sockets in python

I have a problem with sockets, I wanna sent the especifications about my pc in a chat created with sockets, but when I use the sentence send() with the function the compiler throw this error… How can I send that information? Thanks. This is the server code.

#importar librerias
import psutil
import platform
from datetime import datetime
from socket import *
uname = platform.uname()
direccionServidor = "localhost"
puertoServidor = 9099

#Generar un nuevo socket
socketServidor = socket (AF_INET, SOCK_STREAM)

#Se establece conexion
socketServidor.bind((direccionServidor, puertoServidor))
socketServidor.listen()

 

def informacion():
            print("="*40, "System Information", "="*40)
           
            print(f"System: {uname.system}")
            print(f"Node Name: {uname.node}")
            print(f"Release: {uname.release}")
            print(f"Version: {uname.version}")
            print(f"Machine: {uname.machine}")
            print(f"Processor: {uname.processor}")


while True:
    # se establece conexion
    socketConexion, addr = socketServidor.accept()
    print("Conectado con cliente",addr)
    while True:
        #recibe el mensaje del cliente
        mensajeRecibido = socketConexion.recv(4096).decode()
        print(mensajeRecibido)
        if mensajeRecibido == 'informacion':
            socketConexion.send(informacion())

        #condicion de salida
        if mensajeRecibido == 'exit':
            break

        socketConexion.send(input().encode())

    print("Desconectado del cliente",addr)
    #cerramos conexion
    socketConexion.close()

The error:

line 39, in <module>
    socketConexion.send(informacion())
TypeError: a bytes-like object is required, not 'NoneType'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Like this:

def informacion():
    msg = ["="*40, "System Information", "="*40]
    msg.append(f"System: {uname.system}")
    msg.append(f"Node Name: {uname.node}")
    msg.append(f"Release: {uname.release}")
    msg.append(f"Version: {uname.version}")
    msg.append(f"Machine: {uname.machine}")
    msg.append(f"Processor: {uname.processor}")
    return '\n'.join(msg)
...
        socketConexion.send(information().encode())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading