Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with sockets code

Reply
Thread Tools

problem with sockets code

 
 
Daniel
Guest
Posts: n/a
 
      10-02-2008
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.debug(self.__class__.__name__ + ' set data =
True')
while data:
logger_server.debug(self.__class__.__name__ + ' receive
first readline() of data')
data = self.rfile.readline().strip()
logger_server.debug(self.__class__.__name__ + ' first
readline() of data = %s' % data)
total_data.append(data)
receivedCommand = '\n'.join(total_data)

And this is what I have inside my client code

sock=socket.socket(socket.AF_INET,socket.SOCK_STRE AM)
sock.connect(('localhost',5001))

sock.sendall('Hello, 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.
 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      10-03-2008
On Fri, Oct 3, 2008 at 2:13 AM, Daniel <(E-Mail Removed)> wrote:
> 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.debug(self.__class__.__name__ + ' set data =
> True')
> while data:
> logger_server.debug(self.__class__.__name__ + ' receive
> first readline() of data')
> data = self.rfile.readline().strip()
> logger_server.debug(self.__class__.__name__ + ' first
> readline() of data = %s' % data)
> total_data.append(data)
> receivedCommand = '\n'.join(total_data)
>
> And this is what I have inside my client code
>
> sock=socket.socket(socket.AF_INET,socket.SOCK_STRE AM)
> sock.connect(('localhost',5001))
>
> sock.sendall('Hello, 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:
http://hg.shortcircuit.net.au/index....ive/tip.tar.gz

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.sockets 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 KeyboardInterrupt:
break

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

cheers
James


--
--
-- "Problems are solved by method"
 
Reply With Quote
 
 
 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      10-05-2008
In message
<(E-Mail Removed)>, Daniel
wrote:

> while data:
> ...
> data = self.rfile.readline().strip()


Interpreting a random string value as a Boolean is a bad idea.
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      10-05-2008
In article <gca100$q21$(E-Mail Removed)>,
Lawrence D'Oliveiro <(E-Mail Removed)_zealand> wrote:

> In message
> <(E-Mail Removed)>, Daniel
> wrote:
>
> > while data:
> > ...
> > data = self.rfile.readline().strip()

>
> Interpreting a random string value as a Boolean is a bad idea.


Why?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sockets: code works locally but fails over LAN n00m Python 33 09-09-2005 09:27 AM
[Sockets] Problem with ObjectInput/OutputStream Louis Cyphre Java 7 04-13-2005 08:41 AM
Strange problem with System.Net.Sockets.TcpClient() Torsten Brasch ASP .Net 5 01-07-2004 04:51 AM
Problem of throwing an exception (System.Net.Sockets.OverlappedAsyncResult::CompletionPortCallback) VincentWong ASP .Net 1 12-29-2003 09:32 AM
java sockets problem Leo Java 2 09-30-2003 05:21 PM



Advertisments