473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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)
Jul 18 '05 #1
4 6737
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
Jul 18 '05 #2
"Miki Tebeka" <te****@cs.bgu.ac.il> wrote in message
news:33**************************@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
Jul 18 '05 #3
"lebo" <le***********@yahoo.com> wrote in message
news:63**************************@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/

Jul 18 '05 #4
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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Olivier Hoarau | last post by:
Hello, I have build a client/server application with the socket module. The server mades UDP broadcasting and the client only reads UDP broadcast messages. All work fine. Now I want to use for...
3
by: Ergin Aytac | last post by:
I'm trying to run a script written in python and have some socket connection problems. I cutted the origin script (more than 1000 lines) so it is only the part of the connection and there is no...
0
by: Adil Hasan | last post by:
Hello Fred, I just ran across your question. I think that the following code will work: ----- SERVER CODE ------ import SocketServer import time class...
5
by: missiplicity | last post by:
Hi, I am newbie to Python language and am taking my baby steps. I am using Python2.4 from ActiveState on W2K. I am trying to create a simple SocketServer program. Just adding the following 2 lines...
12
by: Paul Rubin | last post by:
Let's say you have a SocketServer with the threading mix-in and you run serve_forever on it. How can you shut it down, or rather, how can it even shut itself down? Even if you use a...
3
by: Magnus Lycka | last post by:
I have a socket server like below which I want to exit when it's out of data. If I interrupt the client, I'll get a broken pipe on the server side, and after a Ctrl-C, I can restart the server...
0
by: Tomi Hautakoski | last post by:
Hello, I'm a Python newbie trying to figure out how to use SocketServer with IPv6. I would like to set up a TCPServer working like below but how to tell SocketServer I need to use AF_INET6? ...
5
by: eliben | last post by:
Hello, I have a small wxPython application. Today I was trying to add some RPC capability to it, so I implemented an instance of SimpleXMLRPCServer that runs in a separate thread when invoked...
4
by: id.engg | last post by:
logFileName = 'log.txt' logfile = open(logFileName, "a") class MyUDPServer(SocketServer.UDPServer): def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8388608)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.