473,407 Members | 2,312 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.

problem with sockets code

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.
Oct 2 '08 #1
3 1461
On Fri, Oct 3, 2008 at 2:13 AM, Daniel <da************@gmail.comwrote:
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"
Oct 3 '08 #2
In message
<f8**********************************@2g2000hsn.go oglegroups.com>, Daniel
wrote:
while data:
...
data = self.rfile.readline().strip()
Interpreting a random string value as a Boolean is a bad idea.
Oct 5 '08 #3
In article <gc**********@lust.ihug.co.nz>,
Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrote:
In message
<f8**********************************@2g2000hsn.go oglegroups.com>, Daniel
wrote:
while data:
...
data = self.rfile.readline().strip()

Interpreting a random string value as a Boolean is a bad idea.
Why?
Oct 5 '08 #4

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

Similar topics

0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
15
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
0
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.