Several problems with Client.py module

0

This comes from before, because I am with a simple client and server project in python, which includes a GUI (wxpython) in the client module. The case is as follows:

Here I present the basic server code:     import socket     import threading

def conexiones(socket_cliente):
    peticion = socket_cliente.recv(1024)
    print ("[*] Mensaje recibido: %s" % peticion)
    socket_cliente.send("HOLA CLIENTE")
    socket_cliente.close()

ip = "0.0.0.0"#para broadcast mi ip
puerto = 5555
max_conexiones = 5
servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

servidor.bind((ip, puerto))
servidor.listen(max_conexiones)


print ("[*] Esperando conexiones en %s:%d" % (ip, puerto))

while True:
    cliente, direccion = servidor.accept()
    print ("[*] Conexion establecida con %s:%d" % (direccion[0],  direccion[1]))
    conexiones = threading.Thread(target=conexiones, args=(cliente,))
    conexiones.start()

As you can see it is a common server waiting for a connection and will notify with a warning. Then I leave the client and I explain my problem (and incidentally if you see more flaws comments are appreciated, since I am a novice in this):

import sys
import wx
import socket

class RedirectText(object):
    def __init__(self,aWxTextCtrl):
        self.out=aWxTextCtrl

    def write(self,string):
        self.out.WriteText(string)

class CLIENT(wx.Frame):
    def __init__(self, *args, **kwargs):
    # Iniciamos el frame
        super(CLIENT, self).__init__(*args, **kwargs)

    # Declaramos servidor y puerto
    # -----------------------------------
    servidor = "127.0.0.1"
    puerto = 5555
    # ----------------------------------

    # Creamos el socket para la conexion e iniciamos conexion
    # ----------------------------------
    cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    cliente.connect((servidor, puerto))
    # ----------------------------------

    # Creamos un panel principal
    self.panel = wx.Panel(self)

    # Creamos los sizers
    # -----------------------------------------------
    self.main_vbox = wx.BoxSizer(wx.VERTICAL)  # caja principal donde situaremos T ODO
    self.form_fgrid = wx.FlexGridSizer(rows=2, cols=2, vgap=5,
                                       hgap=5)  # red sizer de 2 filas 2 columnas y 5 de espacio arriba  abajo y laterales
    # usaremos este sizer para situar dentro las etiquetas y cuadros de texto de Mensaje y Comando
    self.info_hbox = wx.BoxSizer(wx.HORIZONTAL)  # caja para la ventana de informacion de volcado del servidor
    self.button_hbox = wx.BoxSizer(wx.HORIZONTAL)  # caja para situar dentro los tres botones
    # -----------------------------------------------

    # Creamos las etiquetas
    # ------------------------------------------------
    self.mensaje = wx.StaticText(self.panel, wx.ID_ANY, "Mensaje")
    self.comando = wx.StaticText(self.panel, wx.ID_ANY, "Comando")
    # -------------------------------------------------

    # Creamos las cajas de texto
    # -------------------------------------------------
    self.caja_mensaje = wx.TextCtrl(self.panel, wx.ID_ANY)
    self.caja_comando = wx.TextCtrl(self.panel, wx.ID_ANY)
    # -------------------------------------------------

    # Creamos la caja para mostrar la informacion
    # -------------------------------------------------
    self.informacion = wx.TextCtrl(self.panel,wx.ID_ANY, "", wx.Point(-1, -1), wx.Size(-1, -1), wx.TE_MULTILINE)
    # -------------------------------------------------

    # Creamos los botones(x3)
    # -------------------------------------------------
    self.send_mensaje = wx.Button(self.panel, wx.ID_ANY, "Enviar Mensaje", size=(0, 35))
    self.send_comando = wx.Button(self.panel, wx.ID_ANY, "Enviar Comando", size=(0, 35))
    self.send_exit = wx.Button(self.panel, wx.ID_ANY, "Salir", size=(0, 35))
    # --------------------------------------------------

    # Configuramos los sizers
    # -------------------------------------------------
    # Anadimos etiquetas y cajas de texto a su FlexGrid
    self.form_fgrid.AddMany([(self.mensaje, 1),
                             (self.caja_mensaje, 1, wx.EXPAND),
                             (self.comando, 1),
                             (self.caja_comando, 1, wx.EXPAND)])
    self.form_fgrid.AddGrowableCol(1, 1)

    # Anandimos la caja de texto para mostrar la informacion a su HBox
    self.info_hbox.Add(self.informacion, 1, wx.EXPAND)

    # Aadimos los botones a su HBox
    self.button_hbox.AddMany([(self.send_mensaje, 2, wx.EXPAND | wx.RIGHT, 10),
                              (self.send_comando, 2, wx.EXPAND | wx.RIGHT, 10),
                              (self.send_exit, 1, wx.EXPAND)])

    # Anadimos todos los sizers al VBox principal
    self.main_vbox.Add(self.form_fgrid, 0, wx.EXPAND | wx.ALL, 10)
        self.main_vbox.Add(self.info_hbox, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10)
        self.main_vbox.Add(wx.StaticLine(self.panel), 0, wx.EXPAND, 5)
        self.main_vbox.Add(self.button_hbox, 0, wx.EXPAND | wx.ALL, 10)
    # -------------------------------------------------

        self.panel.SetSizer(self.main_vbox)
        self.Centre(True)
        self.Show()

    # Texto de las TextCtrl usado por los botones
    # ----------------------------------
        self.Bind(wx.EVT_BUTTON, self.Funcion_BotonM, id=self.send_mensaje.GetId())
    # vinculamos el boton un evento relacionado con una funcion determinada
        self.Bind(wx.EVT_BUTTON, self.Funcion_BotonC, id=self.send_comando.GetId())
        self.Bind(wx.EVT_BUTTON, self.Funcion_BotonE, id=self.send_exit.GetId())
    # ----------------------------------

    # Mostrar Informacion en 'informacion'
    # --------------------------------------
    '''
    Lograr que la info que enviamos y la que llega del servidor se redireccionen a 
    la caja informacion 
    '''
        respuesta=cliente.recv(4096)

        redir = RedirectText(self.informacion)
        sys.stdout = redir
    # --------------------------------------

    # Funciones para botones
    # -----------------------------------
        def Funcion_BotonM(self, event):
            # volcamos la info de la caja de texto a una variable
            txt = self.caja_mensaje.GetValue()
            cliente.send(txt)

        def Funcion_BotonC(self, event):
            # volcamos la info de la caja de texto a una variable
            txt = self.caja_comando.GetValue()
            cliente.send(txt)

        def Funcion_BotonE(self, event):
            if self.__close_callback__:
                self.__close_callback__()
        # -----------------------------------


if __name__ == "__main__":
    app = wx.App()
    CLIENT(None, -1, "Aplicacion", size=(500, 600))
    app.MainLoop()

The error in question is that the client object does not have the Function_Button_Function attribute. In addition to all this, I am investigating how to redirect information (I want to redirect the information sent by the client to the server and its responses). Thanks in advance and I feel the delay.

    
asked by AlexGA93 11.04.2018 в 17:03
source

0 answers