[Tutor] looping generator

richard kappler richkappler at gmail.com
Thu Jan 7 12:02:17 EST 2016


I have a stream of incoming xml data. I can receive the data, parse the
data, etc, so long as I don't get fancy and I have a miniscule delay in
between each message. If I get rid of the time delay, which I need to, I
need the script to continuously process the incoming messages. Here's what
I have:

#!/usr/bin/env python

import socket
import lxml.etree as ET

def dataRecv(connection):
    print 'receiving'
    while True:
        data = connection.recv(65536)
        while True:
            print "writing to data.in"
            f2.write(data)
            start = data.find('\x02')
            end = data.find('\x03')
            message = data[start+1:end]
            print "writing to messages.out"
            f3.write(message)
            yield message

def dataParse(message):
    print 'parsing'
    xslt = ET.parse('stack13.xsl')
    dom = ET.XML(message)
    transform = ET.XSLT(xslt)
    newdom = transform(dom)
    f1.write(str(newdom))


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_addr = ('', 2008)
#data = sock.makefile('r')
sock.bind(sock_addr)
sock.listen(5)
print 'listening'

f1 = open('parser.out', 'a')
print "opening parser.out"
f2 = open('data.in', 'a')
print "opening data.in"
f3 = open('messages.out', 'a')
print "opening messages.out"

while True:
    # wait for a connection
    connection, client_address = sock.accept()
    q = dataRecv(connection)
    dataParse(q.next())

# close sockrx
#connection.close()

f1.close()


In the dataRecv function, I have tried (where you see while True) if data,
while data and while True. Regardless, it doesn't loop, it receives al ten
messages from the test file being sent, but only processes the first
message then stops (not exits). I feel like I'm missing something obvious
but can't find it.

regards, Richard


More information about the Tutor mailing list