SocketServer class - basis problem

Discussion in 'Python' started by lebo, Jun 24, 2003.

  1. lebo

    lebo Guest

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

    # server
    import SocketServer

    PORT = 8037

    class myRequestHandler(SocketServer.StreamRequestHandler):
    def handle(self):
    self.input = self.rfile.read(1024)
    print self.input
    self.wfile.write("blah")

    server = SocketServer.TCPServer(("", PORT), myRequestHandler)
    print "listening on port", PORT
    server.serve_forever()

    # client
    import socket

    HOST = socket.gethostname()
    PORT = 8037

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    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)
    lebo, Jun 24, 2003
    #1
    1. Advertising

  2. lebo

    Miki Tebeka Guest

    Hello, Leonard,

    > 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.....

    I think that the socket buffer is not full (since you are waiting for
    1024 and "blah" is not enough).

    > this should be easy...(sigh)

    If it was that easy it won't be interesting ;-)

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

    HTH.
    Miki
    Miki Tebeka, Jun 24, 2003
    #2
    1. Advertising

  3. lebo

    Byron Morgan Guest

    Byron Morgan, Jun 24, 2003
    #3
  4. lebo

    Steve Holden Guest

    "lebo" <> wrote in message
    news:...
    > So I'm new to this python stuff - and this has me stumped
    >

    [sample code]
    >
    > 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)


    <gratuitous self-advertisement>
    If you're going to OSCON you might like to sign up for the Python Network
    Programming tutorial - see
    http://conferences.oreillynet.com/cs/os2003/view/e_sess/4165

    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/
    Steve Holden, Jun 24, 2003
    #4
  5. lebo

    Peter Hansen Guest

    Miki Tebeka wrote:
    >
    > Hello, Leonard,
    >
    > > 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.....


    > I think that the socket buffer is not full (since you are waiting for
    > 1024 and "blah" is not enough).


    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
    Peter Hansen, Jun 24, 2003
    #5
    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. NoSf3RaTu
    Replies:
    0
    Views:
    475
    NoSf3RaTu
    Jan 21, 2004
  2. =?Utf-8?B?VGVycnk=?=

    Changing styles sheets on a per user basis

    =?Utf-8?B?VGVycnk=?=, Feb 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    360
    =?Utf-8?B?VGVycnk=?=
    Feb 4, 2004
  3. Steve Franks
    Replies:
    2
    Views:
    1,234
    Steve Franks
    Jun 10, 2004
  4. Replies:
    1
    Views:
    437
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 19, 2005
  5. Replies:
    14
    Views:
    521
    bijeshn
    Apr 8, 2008
Loading...

Share This Page