problem with sockets code

Discussion in 'Python' started by Daniel, Oct 2, 2008.

  1. Daniel

    Daniel Guest

    Hello,

    I can't seem to get my sockets code to work right. Here is what I
    have inside my RequestHandler handle() function:

    total_data=[]

    data = True
    logger_server.debug(self.__class__.__name__ + ' set data =
    True')
    while data:
    logger_server.debug(self.__class__.__name__ + ' receive
    first readline() of data')
    data = self.rfile.readline().strip()
    logger_server.debug(self.__class__.__name__ + ' first
    readline() of data = %s' % data)
    total_data.append(data)
    receivedCommand = '\n'.join(total_data)

    And this is what I have inside my client code

    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect(('localhost',5001))

    sock.sendall('Hello, world\r\n')
    data = sock.recv(1024)
    sock.close()
    print 'Received', repr(data)

    There's a little more to it, but this is enough for me to ask my
    question. The problem is that I can't get the server loop (while
    data:) to stop without closing the connection, but I want to receive
    something back from the server before closing the sockets connection.
    My logs show that the server never leaves the loop.

    Thanks in advance.
    Daniel, Oct 2, 2008
    #1
    1. Advertising

  2. Daniel

    James Mills Guest

    On Fri, Oct 3, 2008 at 2:13 AM, Daniel <> wrote:
    > Hello,
    >
    > I can't seem to get my sockets code to work right. Here is what I
    > have inside my RequestHandler handle() function:
    >
    > total_data=[]
    >
    > data = True
    > logger_server.debug(self.__class__.__name__ + ' set data =
    > True')
    > while data:
    > logger_server.debug(self.__class__.__name__ + ' receive
    > first readline() of data')
    > data = self.rfile.readline().strip()
    > logger_server.debug(self.__class__.__name__ + ' first
    > readline() of data = %s' % data)
    > total_data.append(data)
    > receivedCommand = '\n'.join(total_data)
    >
    > And this is what I have inside my client code
    >
    > sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    > sock.connect(('localhost',5001))
    >
    > sock.sendall('Hello, world\r\n')
    > data = sock.recv(1024)
    > sock.close()
    > print 'Received', repr(data)
    >
    > There's a little more to it, but this is enough for me to ask my
    > question. The problem is that I can't get the server loop (while
    > data:) to stop without closing the connection, but I want to receive
    > something back from the server before closing the sockets connection.
    > My logs show that the server never leaves the loop.
    >
    > Thanks in advance.


    Daniel,

    You really should use an existing framework to help
    you write your application here. You're using the
    plain old (standard-library) sockets module.

    I would suggest you use either Twisted, or pymills.

    Twisted is more feature-rich, and a general purpose
    event-driven framework. It can be a little overwhelming
    to use.

    pymills is my event-driven, component architecture
    library that allows you to build event-driven systems
    very easily with a component design.

    You can download pymills from here:
    http://hg.shortcircuit.net.au/index.wsgi/pymills/archive/tip.tar.gz

    Or you can get the latest developmen branch by using
    Mercurial and cloning it:

    hg clone http://hg.shortcircuit.net.au/index.wsgi/pymills/

    Here is a simple EchoServer that you could modify
    to suit your application needs:

    <code>
    #!/usr/bin/env python

    from pymills import event
    from pymills.event import *
    from pymills.net.sockets import TCPServer

    class Echo(TCPServer):

    @listener("read")
    def onREAD(self, sock, data):
    self.write(sock, data)

    def main():
    echo = Echo(8000)
    event.manager += echo

    while True:
    try:
    manager.flush()
    echo.poll()
    except KeyboardInterrupt:
    break

    if __name__ == "__main__":
    main()
    </code>

    cheers
    James


    --
    --
    -- "Problems are solved by method"
    James Mills, Oct 3, 2008
    #2
    1. Advertising

  3. In message
    <>, Daniel
    wrote:

    > while data:
    > ...
    > data = self.rfile.readline().strip()


    Interpreting a random string value as a Boolean is a bad idea.
    Lawrence D'Oliveiro, Oct 5, 2008
    #3
  4. Daniel

    Roy Smith Guest

    In article <gca100$q21$>,
    Lawrence D'Oliveiro <_zealand> wrote:

    > In message
    > <>, Daniel
    > wrote:
    >
    > > while data:
    > > ...
    > > data = self.rfile.readline().strip()

    >
    > Interpreting a random string value as a Boolean is a bad idea.


    Why?
    Roy Smith, Oct 5, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VincentWong
    Replies:
    1
    Views:
    562
    VincentWong
    Dec 29, 2003
  2. Torsten Brasch
    Replies:
    5
    Views:
    6,230
    Feroze [MSFT]
    Jan 7, 2004
  3. Leo

    java sockets problem

    Leo, Sep 29, 2003, in forum: Java
    Replies:
    2
    Views:
    409
    Steve Horsley
    Sep 30, 2003
  4. Louis Cyphre
    Replies:
    7
    Views:
    1,084
    Esmond Pitt
    Apr 13, 2005
  5. n00m
    Replies:
    33
    Views:
    973
    Bryan Olson
    Sep 9, 2005
Loading...

Share This Page