problem with sockets code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel

    problem with sockets code

    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.d ebug(self.__cla ss__.__name__ + ' set data =
    True')
    while data:
    logger_server.d ebug(self.__cla ss__.__name__ + ' receive
    first readline() of data')
    data = self.rfile.read line().strip()
    logger_server.d ebug(self.__cla ss__.__name__ + ' first
    readline() of data = %s' % data)
    total_data.appe nd(data)
    receivedCommand = '\n'.join(total _data)

    And this is what I have inside my client code

    sock=socket.soc ket(socket.AF_I NET,socket.SOCK _STREAM)
    sock.connect((' localhost',5001 ))

    sock.sendall('H ello, 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.
  • James Mills

    #2
    Re: problem with sockets code

    On Fri, Oct 3, 2008 at 2:13 AM, Daniel <daniel.watrous @gmail.comwrote :
    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.d ebug(self.__cla ss__.__name__ + ' set data =
    True')
    while data:
    logger_server.d ebug(self.__cla ss__.__name__ + ' receive
    first readline() of data')
    data = self.rfile.read line().strip()
    logger_server.d ebug(self.__cla ss__.__name__ + ' first
    readline() of data = %s' % data)
    total_data.appe nd(data)
    receivedCommand = '\n'.join(total _data)
    >
    And this is what I have inside my client code
    >
    sock=socket.soc ket(socket.AF_I NET,socket.SOCK _STREAM)
    sock.connect((' localhost',5001 ))
    >
    sock.sendall('H ello, 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:


    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.soc kets 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 KeyboardInterru pt:
    break

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

    cheers
    James


    --
    --
    -- "Problems are solved by method"

    Comment

    • Lawrence D'Oliveiro

      #3
      Re: problem with sockets code

      In message
      <f84b1539-f5b2-487c-9c2b-75d05c0911cf@2g 2000hsn.googleg roups.com>, Daniel
      wrote:
      while data:
      ...
      data = self.rfile.read line().strip()
      Interpreting a random string value as a Boolean is a bad idea.

      Comment

      • Roy Smith

        #4
        Re: problem with sockets code

        In article <gca100$q21$6@l ust.ihug.co.nz> ,
        Lawrence D'Oliveiro <ldo@geek-central.gen.new _zealandwrote:
        In message
        <f84b1539-f5b2-487c-9c2b-75d05c0911cf@2g 2000hsn.googleg roups.com>, Daniel
        wrote:
        >
        while data:
        ...
        data = self.rfile.read line().strip()
        >
        Interpreting a random string value as a Boolean is a bad idea.
        Why?

        Comment

        Working...