473,800 Members | 3,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.d ebug(self.__cla ss__.__name__ + ' set data =
True')
while data:
logger_server.d ebug(self.__cla ss__.__name__ + ' receive
first readline() of data')
data = self.rfile.read line().strip()
logger_server.d ebug(self.__cla ss__.__name__ + ' first
readline() of data = %s' % data)
total_data.appe nd(data)
receivedCommand = '\n'.join(total _data)

And this is what I have inside my client code

sock=socket.soc ket(socket.AF_I NET,socket.SOCK _STREAM)
sock.connect((' localhost',5001 ))

sock.sendall('H ello, 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 1477
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.d ebug(self.__cla ss__.__name__ + ' set data =
True')
while data:
logger_server.d ebug(self.__cla ss__.__name__ + ' receive
first readline() of data')
data = self.rfile.read line().strip()
logger_server.d ebug(self.__cla ss__.__name__ + ' first
readline() of data = %s' % data)
total_data.appe nd(data)
receivedCommand = '\n'.join(total _data)

And this is what I have inside my client code

sock=socket.soc ket(socket.AF_I NET,socket.SOCK _STREAM)
sock.connect((' localhost',5001 ))

sock.sendall('H ello, 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.soc kets 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 KeyboardInterru pt:
break

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

cheers
James
--
--
-- "Problems are solved by method"
Oct 3 '08 #2
In message
<f8************ *************** *******@2g2000h sn.googlegroups .com>, Daniel
wrote:
while data:
...
data = self.rfile.read line().strip()
Interpreting a random string value as a Boolean is a bad idea.
Oct 5 '08 #3
In article <gc**********@l ust.ihug.co.nz> ,
Lawrence D'Oliveiro <ld*@geek-central.gen.new _zealandwrote:
In message
<f8************ *************** *******@2g2000h sn.googlegroups .com>, Daniel
wrote:
while data:
...
data = self.rfile.read line().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
1451
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 framework, let us call it that, consists in two parts. The first part is just a basic thread class deriving from threading.Thread with a few extra functionality that makes it easier for a thread to spawn a new thread and "father it". Each of its
1
3799
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 pending connections every 500ms. I also have a vb6 application that uses the WinSock control at the other end of the communication tunel. I have to work with vb6 here because it uses less memory than .NET.
15
2215
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 now it's throwing this stream of errors right away. DOes anyone have any idea what they mean. I havn't changed the source at all since it was working (which was for four days). Unhandled Exception: System.TypeInitializationException: The type...
0
1310
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 problems: 1) I cannot run the client without administrator privileges. If I try I get the following message: Unhandled Exception: System.Security.SecurityException: Requested registry access is not allowed. at...
0
3925
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 development client. Bur Its gives me following error message. I searched lots of things on net and tried on remote server but i didnt got suceess. Can any one tell me how to set TCP\IP connection protocol on server for particular instance. Becuase I think
5
24023
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 development client. Bur Its gives me following error message. I searched lots of things on net and tried on remote server but i didnt got suceess. Can any one tell me how to set TCP\IP
1
17552
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 another program for information passing. Upon receiving a particular "command" from the the information passing program, myProblemProgram will launch a separate thread to do individual communication with another file transfer program. The thread...
1
20647
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 I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
0
9553
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10256
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10039
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9095
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6824
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4152
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.