python-2.7socketsmulticastsocket

multiasting TypeError: argument must be an int, or have a fileno() method


I have a problem, I've created multicasting with gui and my program need sys.stdin but I have text - string in my gui.

sockets_list = [sys.stdin, server] 
read_sockets,write_socket, error_socket  = select.select(sockets_list,[],[])

This code does not work

def message(self, event):
    mess = self.write.get() 
    self.sockets_list = [mess, self.server]
    self.read_sockets, write_socket, error_socket  = select.select(self.sockets_list,[],[]) 
    print self.sockets_list
    self.write.delete(0, Tkinter.END)
    if mess != '':
      self.mess1.configure(state = NORMAL)
      self.all_users.append('Adam')
      for socks in self.read_sockets:
        if socks == self.server:
            message = socks.recv(2048)
            self.mess1.insert(Tkinter.END, '%s> %s \n' %(self.nick, message)) #wyswietla wprowadzony tekst
            self.mess1.see(Tkinter.END) 
        else:
            message = mess
            self.server.send(message)
            print self.nick
            print message
      self.mess1.see(Tkinter.END) 
      self.mess1.configure(state = DISABLED)

I want to create a messenger on a multicast basis I send a message from one client - I go to the server and send it to the rest of the clients. The problem is that I can not use the message entered in the gui and it works in the terminal

All code

web_pr_less2_client.py - client

dup.py - serv

https://github.com/Antoni-Hp/Web_programing/tree/master/Python_web_programing_less2


Solution

  • The problem here as stated by the author of the question is that "the code doesn't work". While the author is trying to find a solution for a particular error that results in a particular exception throw, fixing it alone won't solve the problem of the code not working. It also appears that the author is only beginning to learn how to code GUI applications.

    I have little knowledge with Tkinter, but I'll try to help regardless by explaining what problems I see here.

    1. The message method as presented in the question is being called in response to a "submit message" event. However, this method also tries to read the messages that may arrive from the server at any given time. This means that the user will not see any new messages from the server until they themselves send a message. This "read from server" piece of code should be moved out from this method and handled separately.

      Now, as I stated before I'm not familiar with Tkinter so I can't claim that I know what's the best approach here, but normally with GUI apps listening to the background events needs to be done by either awaiting for an arrival of a "ready" event through a callback function or by polling for the events on which we await in the background. How this is achieved depends on the used technology (timers, threads, OS API functions with callbacks).

      In your case:

      1.1. Extract the read operations to a separate method.
      1.2. Use a Tkinter timer to call this method periodically.
      1.3. select() on the socket with a short timeout (try 0.01)
      1.4. If socket is ready for reading, read it and append it to the chat window.

      This is a primitive solution as the 0.01 timeout will stall your application and in a production-grade software should not be done in the main thread, but it's a good starting point.

    2. The exception throw on message submit is caused by trying to use the mess variable you obtain from the text input widget as if it was an I/O handle, while in actuality it's a str. I/O handles are files, file descriptors, sockets, pipes. str is a data type and it cannot be passed to select(). However, you can just send it through the socket without much care, ie.

      def message(self, event):
          message = self.write.get()
          self.write.delete(0, Tkinter.END)
          self.server.send(message)
          self.mess1.configure(state=NORMAL)
          self.mess1.insert(Tkinter.END, '%s> %s \n' %(self.nick, message))
          self.mess1.see(Tkinter.END)
          self.mess1.configure(state=DISABLED)
      

    Also, aside from the problem at hand, my general recommendations are:

    Your code doesn't follow PEP8 styling recommendations. While this doesn't affect your software functionally, you may reconsider following those rules. They are officially recommended by the authors of the Python language and it will be very likely that most of the Python code you find online will follow them. Running a flake8 check over your code shows 351 violations.

    I also recommend to focus on a more precise naming of your symbols.