Here is what I have for my server (This code is really not that pretty, I apologize):
import socket
import SocketServer
class CommonServer(SocketServer.ThreadingTCPServer):
def server_bind(self):
"""Override server_bind to store the server name."""
SocketServer.TCPServer.server_bind(self)
host, port = self.socket.getsockname()
if not host or host == '0.0.0.0':
host = socket.gethostname()
hostname, hostnames, hostaddrs = socket.gethostbyaddr(host)
if '.' not in hostname:
for host in hostnames:
if '.' in host:
hostname = host
break
self.server_name = hostname
self.server_port = port
print self.server_name
print port
class CommonRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
print "Handling request"
self.wfile.write('000 Connected ')
datastart = 0
while 1:
print "looping"
s = self.rfile.readline()
print "Message:" + s
if s[-2:] == ' ': s = s[:-2]
elif s[-1:] == ' ': s = s[:-1]
s = s[0:4]
if s == 'QUIT':
print "Quit"
self.wfile.write('000 Bye\n')
break
elif s == 'NAME':
print "Name"
self.wfile.write('000 My name is Python\n')
elif s == 'DATA':
print "Data"
self.wfile.write('000 Data Message\n' )
else:
print "Unknown"
self.wfile.write('200 Unknown Command\n')
def setup(self):
SocketServer.StreamRequestHandler.setup(self)
def test():
portnum = 8001
comserver = CommonServer(('', portnum), CommonRequestHandler )
comserver.handle_request()
print "Running"
if __name__ == '__main__':
test()
Here is what I have for my client:
ASYNC_POLL_LENGTH = 0.05
class Connection(asyncore.dispatcher):
def __init__ (self, server, socket_num):
self.server_name = str( server )
self.port_num = socket_num
self.message_queue = []
self.current_message = 0
self.rcv_data = ""
asyncore.dispatcher.__init__(self)
self.create_socket( socket.AF_INET, socket.SOCK_STREAM )
self.connect(( self.server_name, self.port_num ))
def handle_connect( self ):
print "CONNNECTED"
# collect some more finger server output.
def handle_read_event( self ):
print "handle_read_event received"
self.rcv_data = ""
more = self.recv(512)
print "More Data: %s" % more
self.rcv_data = self.rcv_data + more
print "RCV Data: %s" % self.rcv_data
if self.current_message:
print "Message: %s Type: %s RCV Data: %s" % (self.current_message['data'], self.current_message['type'], self.rcv_data )
self.current_message['callback']( self.current_message, self.rcv_data )
# the other side closed, we're done.
def handle_close( self ):
print 'Handling Close Event'
self.connected_callback( False )
def handle_write( self ):
# If there is a message in the queue
if len( self.message_queue ) > 0:
# Pop the first message off the queue
self.current_message = self.message_queue.pop(0)
print "Sending Message: %s" % self.current_message['data']
self.send(self.current_message['data'] + "\n")
def queue( self, message ):
self.message_queue.append( message )
print "Message Queued: %s" % message['data']
#========================================================================================
class TestAppClient:
def __init__( self, server, socket ):
self.server = server
self.socket = socket
self.nomad = Connection( server, socket )
asyncore.loop(count=1)
Then I use a timer to call "asyncore.loop(count=1)" every 10ms or on the IDLE loop of my GUI.
The line in red is what I have to append the '\n' to in order for my server to get it.
Thanks in advance,
John
On 10/31/05, Steve Holden <[EMAIL PROTECTED]> wrote:
John W wrote:
> Hello,
>
> I have a test environment that consists of a server and an application that
> opens an asyncore socket and communicates with it.
>
> In my application, on the handle_write call, if I do not append a "\n" to my
> message the server never gets the message. If I do append it, the server can
> read the message and act accordingly.
>
> So my call looks like this:
> self.send( message] + "\n")
>
> Is this how it was designed? Do you have to send a '\n' to have the data
> read? I can't find a whole lot of documentation on the send method.
>
> If you could offer some help, I would appreciate it.
>
The short answer to your question is "no, send() does not require
newline". But much depends on your protocol design and on explicit
details of your client and server software. You'd probably have to post
a bit more code before we could say why you are seeing what you are seeing.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
-- http://mail.python.org/mailman/listinfo/python-list