Return a string result with out breaking loop

A

Andrew

Hi I was wondering if there is anyway with XML RPC to send a string of
text from the server to the client with out calling return thus breaking
my loop

for example

def somefunc():
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
out2 = str(full_filename) + " " + str(theact)
return out2

the return statement will return a result breaking my loop. My goal is
to have it continue looping and updating the client

any ideas?

Cheers

Andrew

Thanks in advance

Pyhon 2.5.2

Windows XP
 
A

Andrew

Hi Ive been trying with yield all day and other different things :D

but I always get the same result using yield

Fault: said:
> objects">

I'm not sure exactly what I am doing wrong as this is the first time Ive
used yield

Any suggestions on how to fix this error

Cheers

Andrew
 
C

cnb

def somefunc():
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
yield str(full_filename) + " " + str(theact)



?


Here is an example if that doesn't work, using yield, to show how to
use yield:
def yi(x):
while x > 0:
yield str(x)
x -= 1


Traceback (most recent call last):
File "<pyshell#151>", line 1, in <module>
a.next()
StopIteration
 
C

cnb

def somefunc():
       for action, files in results:
               full_filename = os.path.join(path_to_watch, files)
               theact = ACTIONS.get(action, "Unknown")
               yield  str(full_filename) +  " " + str(theact)

?

Here is an example if that doesn't work, using yield, to show how to
use yield:
def yi(x):
        while x > 0:
                yield str(x)
                x -= 1


Traceback (most recent call last):
  File "<pyshell#151>", line 1, in <module>
    a.next()
StopIteration


you can also do:
def yi(x):
while x > 0:
yield str(x)
x -= 1 print x


10
9
8
7
6
5
4
3
2
1
 
A

Andrew

Yes I found many examples similiar to that but Yield still produces that
error

Sorry if I sound Novice but I have no formal education in programming so
understanding something takes me a bit longer than most of you guys :-D

Ive included a bit more of the function

How ever unimportant it may be


def watchos(self, path_to_watch="C:\\"):
FILE_LIST_DIRECTORY = 0x0001
try: path_to_watch or "."
except: path_to_watch = "."
path_to_watch = os.path.abspath(path_to_watch)

out1 = "Watching %s at %s" % (path_to_watch, time.asctime()) + "\n\n"
while 1:

#INSERT MORE CODE HERE

results = change_handle
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
yield str(full_filename) + " " + str(theact)

But I am having a hard time wrapping my head around yield... perhaps if
it produced more than the same error no matter what I did. I could try
and get a bit further

Anyway once again I appreciate the help cheers

:)
 
C

cnb

ok post exactly what you do when this hapens:
ault: said:
> objects">

it complains you are trying to marshal a generator object rather than
the file you are yielding.



also, something I am not sure about:
try: open("C:/ruby/progs/blandat/infixtoprefix.rb") or open(str(x))
except:print "hello"

try: (open(str(x)) or open("C:/ruby/progs/blandat/infixtoprefix.rb"))
except:print "hello"


the or doesnt seem to work as expected. but thats not the problem youa
re experienceing
 
A

Andrew

I run the server then execute the client. From the client I execute the
function key.watchos()

My goal is an administration tool

and this is just a function in my goal :)



Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Maboroshi>python C:\software\mabssystem\client.py
Use the keyword, "key" to interact with the serverTraceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python25\lib\xmlrpclib.py", line 1147, in __call__
return self.__send(self.__name, args)
File "C:\Python25\lib\xmlrpclib.py", line 1437, in __request
verbose=self.__verbose
File "C:\Python25\lib\xmlrpclib.py", line 1201, in request
return self._parse_response(h.getfile(), sock)
File "C:\Python25\lib\xmlrpclib.py", line 1340, in _parse_response
return u.close()
File "C:\Python25\lib\xmlrpclib.py", line 787, in close
raise Fault(**self._stack[0])
Fault: said:
> objects">

the function watchos is basically based off of Tim Goldens Directory
watching app

the function is as follows in entirety

def watchos(self, path_to_watch="C:\\"):
#get path or maintain current path of app
FILE_LIST_DIRECTORY = 0x0001
try: path_to_watch or "."
except: path_to_watch = "."
path_to_watch = os.path.abspath(path_to_watch)

out1 = "Watching %s at %s" % (path_to_watch, time.asctime()) +
"\n\n"
# FindFirstChangeNotification sets up a handle for watching
# file changes.
while 1:

hDir = win32file.CreateFile (
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)

change_handle = win32file.ReadDirectoryChangesW (
hDir,
1024,
True,#Heap Size include_subdirectories,
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_SIZE |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY,
None,
None
)

# Loop forever, listing any file changes. The WaitFor... will
# time out every half a second allowing for keyboard
interrupts
# to terminate the loop.
ACTIONS = {
1 : "Created",
2 : "Deleted",
3 : "Updated",
4 : "Renamed from something",
5 : "Renamed to something"
}

results = change_handle
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
yield str(full_filename) + " " + str(theact)


and the rpc client is as follows

import xmlrpclib, code

# Specify IP of Server here defualt port is 9000
url = "http://127.0.0.1:8888"
sock = xmlrpclib.ServerProxy(url, allow_none=True, verbose=0)
# use the interactive console to interact example "key.keyit()" will
start the logger
interp = code.InteractiveConsole({'key': sock})
interp.interact("Use the keyword, \"key\" to interact with the server")

Sorry if I posted to much code :)

Thank you in advance
 
A

alex23

I run the server then execute the client. From the client I execute the
function key.watchos()
Sorry if I posted to much code :)

The problem is the Python implementation of XML-RPC _can't_ send
generators (which is what the 'unable to marshal' error is telling
you). From the docs:

"Types that are conformable (e.g. that can be marshalled through XML),
include the following (and except where noted, they are unmarshalled
as the same Python type): [boolean, integers, floating-point numbers,
strings, arrays, structures (dicts), dates, binary dates]"

I'm not sure how to easily fix this to achieve what you want, though.
 
C

castironpi

Hi I was wondering if there is anyway with XML RPC to send a string of
text from the server to the client with out calling return thus breaking
  my loop

for example

  def somefunc():
       for action, files in results:
               full_filename = os.path.join(path_to_watch, files)
               theact = ACTIONS.get(action, "Unknown")
               out2 =  str(full_filename) +  " " + str(theact)
               return out2

the return statement will return a result breaking my loop. My goal is
to have it continue looping and updating the client

any ideas?

Cheers

Andrew

Thanks in advance

Pyhon 2.5.2

Windows XP

Andrew,

I have generators working on both sides of a SimpleXMLRPCServer
instance. You ask for a server-side generator, and I won't post the
whole program, as it's long, so here's an important part:

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_introspection_functions()

def gen( ):
while 1:
yield 1
yield 2
yield 3

servg= gen( )
server.register_function( servg.next, 'gen' )

Called 's.gen( )' four times, output:

localhost - - [25/Aug/2008 23:08:28] "POST /RPC2 HTTP/1.0" 200 -
1
localhost - - [25/Aug/2008 23:08:29] "POST /RPC2 HTTP/1.0" 200 -
2
localhost - - [25/Aug/2008 23:08:30] "POST /RPC2 HTTP/1.0" 200 -
3
localhost - - [25/Aug/2008 23:08:31] "POST /RPC2 HTTP/1.0" 200 -
1

As expected. I guess that your problem was on this line:

server.register_function( servg.next, 'gen' )

where instead you just wrote

server.register_function( servg, 'gen' ) #wrong

or perhaps even

server.register_function( gen, 'gen' ) #wrong

I'll offer an explanation of the difference too, for the asking.
 
A

Andrew

Hi I have done this without error

:D

Yield returns the result I am looking for... however it does not
continue looping. It does the same thing as return would

any suggestions

my code is as follows

serveraddr = ('', 8888)
srvr = ThreadingServer(serveraddr, SimpleXMLRPCRequestHandler,
allow_none=True)

srvr.register_instance(theApp())

srvr.register_introspection_functions()

def watchos(path_to_watch="C:\\"):
#get path or maintain current path of app
FILE_LIST_DIRECTORY = 0x0001
try: path_to_watch or "."
except: path_to_watch = "."
path_to_watch = os.path.abspath(path_to_watch)

out1 = "Watching %s at %s" % (path_to_watch, time.asctime()) + "\n\n"
# FindFirstChangeNotification sets up a handle for watching
# file changes.
while 1:

hDir = win32file.CreateFile (
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)

change_handle = win32file.ReadDirectoryChangesW (
hDir,
1024,
True,#Heap Size include_subdirectories,
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_SIZE |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY,
None,
None
)

# Loop forever, listing any file changes. The WaitFor... will
# time out every half a second allowing for keyboard interrupts
# to terminate the loop.
ACTIONS = {
1 : "Created",
2 : "Deleted",
3 : "Updated",
4 : "Renamed from something",
5 : "Renamed to something"
}

results = change_handle
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
out2 = str(full_filename) + " " + str(theact)
yield str(out2)

servg = watchos()
srvr.register_function(servg.next, 'watchos')

srvr.register_multicall_functions()
srvr.serve_forever()
 
F

Fredrik Lundh

Andrew said:
Yield returns the result I am looking for... however it does not
continue looping. It does the same thing as return would

the XML-RPC protocol doesn't really support your use case; for each
call, the client issues a complete request package, and the server
produces a complete response in return.

</F>
 
A

Andrew

mmk guess I will have to look for alternate solutions for this project.
Thank you all for your help

cheers Andrew!
 
C

castironpi

         results = change_handle
         for action, files in results:
             full_filename = os.path.join(path_to_watch, files)
             theact = ACTIONS.get(action, "Unknown")
             out2 = str(full_filename) +  " " + str(theact)
         yield str(out2)

I think you have the yield in the wrong place. Should you tab it over
one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top