Home > database >  Send a def with sockets in python
Send a def with sockets in python

Time:10-06

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'

CodePudding user response:

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())
  • Related