SocketServer class - basis problem

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

    SocketServer class - basis problem

    So I'm new to this python stuff - and this has me stumped

    # server
    import SocketServer

    PORT = 8037

    class myRequestHandle r(SocketServer. StreamRequestHa ndler):
    def handle(self):
    self.input = self.rfile.read (1024)
    print self.input
    self.wfile.writ e("blah")

    server = SocketServer.TC PServer(("", PORT), myRequestHandle r)
    print "listening on port", PORT
    server.serve_fo rever()

    # client
    import socket

    HOST = socket.gethostn ame()
    PORT = 8037

    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.connect((HOST , PORT))
    s.send('Hello, world')

    # Fails
    data = s.recv(1024)

    s.close()
    print 'Received', data

    Why does s.recv() hang the client? It seems like server is not
    handing back "blah", but I'm sure it is.....this should be
    easy...(sigh)
  • Miki Tebeka

    #2
    Re: SocketServer class - basis problem

    Hello, Leonard,
    [color=blue]
    > data = s.recv(1024)
    > s.close()
    > print 'Received', data
    >
    > Why does s.recv() hang the client? It seems like server is not
    > handing back "blah", but I'm sure it is.....[/color]
    I think that the socket buffer is not full (since you are waiting for
    1024 and "blah" is not enough).
    [color=blue]
    > this should be easy...(sigh)[/color]
    If it was that easy it won't be interesting ;-)

    Have a look at http://www.amk.ca/python/howto/sockets/

    HTH.
    Miki

    Comment

    • Byron Morgan

      #3
      Re: SocketServer class - basis problem

      "Miki Tebeka" <tebeka@cs.bgu. ac.il> wrote in message
      news:33803989.0 306232125.1cd45 6c2@posting.goo gle.com...[color=blue]
      >
      > Have a look at http://www.amk.ca/python/howto/sockets/
      >
      > HTH.
      > Miki[/color]

      And then check out



      Comment

      • Steve Holden

        #4
        Re: SocketServer class - basis problem

        "lebo" <leonardbocock@ yahoo.com> wrote in message
        news:63701d2f.0 306231709.7990d 916@posting.goo gle.com...[color=blue]
        > So I'm new to this python stuff - and this has me stumped
        >[/color]
        [sample code][color=blue]
        >
        > Why does s.recv() hang the client? It seems like server is not
        > handing back "blah", but I'm sure it is.....this should be
        > easy...(sigh)[/color]

        <gratuitous self-advertisement>
        If you're going to OSCON you might like to sign up for the Python Network
        Programming tutorial - see


        This tutorial is intended to help network programming beginners to write
        their own networking code.
        </gratuitous self-advertisement>

        regards
        --
        Steve Holden http://www.holdenweb.com/
        Python Web Programming http://pydish.holdenweb.com/pwp/



        Comment

        • Peter Hansen

          #5
          Re: SocketServer class - basis problem

          Miki Tebeka wrote:[color=blue]
          >
          > Hello, Leonard,
          >[color=green]
          > > data = s.recv(1024)
          > > s.close()
          > > print 'Received', data
          > >
          > > Why does s.recv() hang the client? It seems like server is not
          > > handing back "blah", but I'm sure it is.....[/color][/color]
          [color=blue]
          > I think that the socket buffer is not full (since you are waiting for
          > 1024 and "blah" is not enough).[/color]

          This is not how the size parameter to recv() is used. It is a
          *maximum*, meaning any amount of data from zero bytes (if the
          socket is closed) to that value will be returned, with *no*
          guarantees how much is actually returned. Often, but absolutely
          not always, you will get a whole "line" and newbies will be
          deceived into thinking it's enough just to recv(1024) and
          carry on, but eventually such code will fail.

          -Peter

          Comment

          Working...