Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Python Why is this String Sent Twice
  • Xin
    Posts: 3,251
    import asyncore
    import socket

    class EchoHandler(asyncore.dispatcher_with_send):

    def handle_read(self):
    self.send('Welcome\r\n')
    data = self.recv(8192)
    if data == 'help':
    self.send('Commands:\r\nuser\r\npass\r\n')
    elif data == 'user':
    self.send('Login\r\n')
    else:
    self.send('Invalid Command\r\n')




    class EchoServer(asyncore.dispatcher):

    def __init__(self, host, port):
    asyncore.dispatcher.__init__(self)
    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
    self.set_reuse_addr()
    self.bind((host, port))
    self.listen(5)

    def handle_accept(self):
    pair = self.accept()
    if pair is None:
    pass
    else:
    sock, addr = pair
    print 'Incoming connection from %s' % repr(addr)
    handler = EchoHandler(sock)


    server = EchoServer('localhost', 8080)
    asyncore.loop()


    This is a multi user socket listener im trying to make, when i send something that calls the invalid command thing it returns two invalid commands eg

    >> tautat
    Invalid Command
    Invalid Command


    Can anyone help me find out why it does this?
    Xin
  • Sh3llc0d3
    Posts: 1,910
    I don't suppose you are using a client you've made? If you are can you post the source?... I don't know python but it could be an issue with the sending of the invalid command and it's sending it twice and returning the error twice...?
  • chroniccommand
    Posts: 1,389
    Hm I don't know much about asyncore. But I'll try and fix the code up for you and clean it up.

    import asyncore
    import socket

    class EchoHandler(asyncore.dispatcher_with_send):

    def HandleRead(self): #Changed handle_read to HandleRead. More pythonic
    self.send('Welcome\r\n')
    data = self.recv(8192)
    if data == 'help':
    self.send('Commands:\r\nuser\r\npass\r\n')
    elif data == 'user':
    self.send('Login\r\n')
    else:
    self.send('Invalid Command\r\n')




    class EchoServer(asyncore.dispatcher):

    def __init__(self, host, port):
    asyncore.dispatcher.__init__(self)
    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
    self.set_reuse_addr()
    self.bind((host, port))
    self.listen(5)

    def HandleAccept(self):
    pair = self.accept()
    if pair is None:
    pass
    else:
    sock, addr = pair
    print 'Incoming connection from %s' % repr(addr)
    handler = EchoHandler(sock)


    server = EchoServer('localhost', 8080)
    asyncore.loop()


    Where did you get this source from? Also could you explain what asyncore is?
  • Xin
    Posts: 3,251
    Just using raw putty as client.
    Xin