Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   SocketServer class - basis problem (http://www.velocityreviews.com/forums/t318771-socketserver-class-basis-problem.html)

lebo 06-24-2003 01:09 AM

SocketServer class - basis problem
 
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)

Miki Tebeka 06-24-2003 05:25 AM

Re: SocketServer class - basis problem
 
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

Byron Morgan 06-24-2003 06:44 AM

Re: SocketServer class - basis problem
 
"Miki Tebeka" <tebeka@cs.bgu.ac.il> wrote in message
news:33803989.0306232125.1cd456c2@posting.google.c om...
>
> Have a look at http://www.amk.ca/python/howto/sockets/
>
> HTH.
> Miki


And then check out
http://aspn.activestate.com/ASPN/Coo.../Recipe/200946



Steve Holden 06-24-2003 12:13 PM

Re: SocketServer class - basis problem
 
"lebo" <leonardbocock@yahoo.com> wrote in message
news:63701d2f.0306231709.7990d916@posting.google.c om...
> 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...ew/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/




Peter Hansen 06-24-2003 01:42 PM

Re: SocketServer class - basis problem
 
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


All times are GMT. The time now is 04:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57