[Tutor] XML-RPC data transfers.

Luke Paireepinart rabidpoobear at gmail.com
Sun Dec 31 01:08:47 CET 2006


Chris Hengge wrote:
> if I'm trying to transmit a 'file' that is actually saved on the HD, 
> the code from the link in my first post works fine (reading in the 
> file using binary mode access). My problem is that I'm trying to 
> figure out how to transfer data that isn't yet saved to the drive, 
> because I'm wanting to be able to send any sort of data type across 
> the connection. This is getting rather frustrating because I'm getting 
> so many replies back that aren't even attempts at answers, but rather 
> questions about things I'm not trying to do. I'll restate the problem.
>
> grab image data from server side connection.
> transfer image data to client side
> write image data to file on client side.
>
> I've already established a working RPC connection, and if I transmit a 
> 'file' that can be read using binary mode (exact same code as posted 
> in both the link in my first email, and by others in this thread) it 
> works fine. My problem I'm trying to overcome is that I
>
> ***do not want to write the image data from the server to a file, just 
> to send it, then delete the image file on the server***
>
And how...

It's clear what you're trying to do.
The problem is this:

1) ImageGrab.grab() -> returns image object that has a 'save' method.
You use this method with the following syntax
import Image, ImageGrab
im = ImageGrab.grab()
im.save('filename.jpg')

to save the file .
2)
you read back in the file by doing:
f = file('filename.jpg','rb')
contents = f.read()


3)
you send it with this:
xmlrpclib.Binary(contents)


What you're trying to do is to eliminate step 1 and 2, so that you read 
the ImageGrab's data directly into the xmlrpclib.Binary method call
without having to write a file.

Okay.
So the main problem we're having here is that you seem to think that you 
should be able to send the ImageGrab object itself over the connection.
What is the problem is that xmlrpclib doesn't understand these class 
instances.  you need to give it plain binary data that it can send.
So there's a few ways you can go about this.

But the main strategy is to get the data out of the ImageGrab object.
one way is stated above - use the save method to write to a file.
another possible way is to create a filelike class, implementing 'tell' 
'seek' and 'write' methods, that just collects all the data written to 
it and keeps it in memory
rather than writing it out to a file.
This is hinted at in the documentation for im.save():
"You can use a file object instead of a filename. In this case, you must 
always specify the format. The file object must implement the *seek*, 
*tell*, and *write* methods, and be opened in binary mode."
(I assume it means you can use a file-like object, also, but it's 
possible that you can't, and I leave it to you to test that :) )

a third solution is to convert the data to a string using
*im.tostring().
*send that over your connection, and use im.fromstring() on the other 
end to recreate the image.

you could even use im.getdata(), iterate over it, construct your own 
representation of the data, and convert this back to an image at the 
other end.

All of these strategies have nothing at all to do with xmlrpc.
I have no idea what xmlrpc is, but I don't need to.
Your question is simplified to:
How do I convert a PIL Image instance into binary data?

As Kent said:
"If you are writing your image data to
a file, then using something like Lee's example below, that just reads
the file data into a Binary object, you should be able to just create
the Binary object from the image data directly."

 > For this specific challenge, I've found no actual "how-to" help... 
Just bits of information making it sound possible. Again, I dont want to 
"file transfer" anything, I want to send data that isn't in the default 
data-types for xml-rpc, which I've read can be done using binary mode 
transfers.

The reason you haven't had any actual 'how-to' help is because we 
believe that you could figure out what to do from the information given 
to you.
One of the greatest things about learning programming is figuring out 
how to do things yourself.
That being said, this mailing list's purpose is to help you when you get 
stuck along the way.
Not to give you code snippets that do exactly what you want - just to 
give you that little nudge in the right direction so you can continue 
working things out for yourself.
An example of this - I was playing Twilight Princess last night, and I 
got stuck in this one area.  I couldn't figure out what to do, and I ran 
around for an hour trying to find out what to do next.  I finally asked 
the in-game character that follows you around for help - and she gave me 
a hint.
I went to the place she told me to (I'm trying to avoid spoilers) and I 
did what she suggested - looking more closely at something.
After a good 30 seconds, it all of the sudden hit me.  I knew exactly 
what to do, and I went on happily through the game.

The point is that the most enjoyable part of the game is when you figure 
out something like that.  I had all the clues right there, I just 
couldn't piece them together.

So we've given you clues.  Kent suggested - try to get the image data 
directly without going through a save-open process.
You already know how to send binary data.  So how do you get binary data 
from the image?
That's what we wanted you to figure out.
well, as I said before, you can try to pass save() a file-like object 
and keep it in memory,
or, probably a much better solution, is just to convert it to a string.

Also, why are you using xml?  you're still working on that VNC right?
Why not just plain sockets, or twisted, or something?

HTH,
-Luke
>
> On 12/30/06, *Kent Johnson* <kent37 at tds.net <mailto:kent37 at tds.net>> 
> wrote:
>
>     Chris Hengge wrote:
>     > I might have been unclear, or this tid-bit might have been lost
>     in the
>     > thread... but I'm trying to send directly from ImageGrab.Grab(),
>     without
>     > saving the data as a file. Thats where I'm getting hung... If it
>     try to
>     > send an actual stored file, I have no problem. Is this maybe
>     impossible?
>     > My thought was that I could just save a little process time and file
>     > fragmentation if I cut out the middle man, plus there really is no
>     > reason to save the screen capture on the server side.
>
>     Can you show the code that works? If you are writing your image
>     data to
>     a file, then using something like Lee's example below, that just reads
>     the file data into a Binary object, you should be able to just create
>     the Binary object from the image data directly.
>
>     Kent
>
>     >
>     > Maybe I really need to look into SOAP for this sort of stuff?
>     I'm just
>     > playing with the technology, and from the searching I've done, the
>     > XML-RPC seemed to fit my needs best. I could certainly be wrong
>     though.
>     >
>     > Thanks for both of you giving me feedback.
>     >
>     > On 12/29/06, *Lee Harr* <missive at hotmail.com
>     <mailto:missive at hotmail.com>
>     > <mailto:missive at hotmail.com <mailto:missive at hotmail.com>>> wrote:
>     >
>     >      >
>     http://www.velocityreviews.com/forums/t343990-xmlrpc-send-file.html
>     >     <
>     http://www.velocityreviews.com/forums/t343990-xmlrpc-send-file.html>
>     >      >
>     >      >Using this example I get error's about 'expected binary
>     .read(),
>     >     but got
>     >      >instance instead.
>     >
>     >
>     >     I assume you are using this ...
>     >
>     >      >d = xmlrpclib.Binary(open("C:\\somefile.exe").read())
>     >
>     >
>     >     Are you using windows?
>     >
>     >     I think you would need to pass the binary flag to open ...
>     >
>     >             imagedata = open(filename, 'rb').read()
>     >
>     >
>     >
>     >     It's probably a good idea to use the binary flag if you are
>     expecting
>     >     binary data just in case it gets ported somewhere else later.
>     >
>     >
>     >      >I've just been using xmlrpclib and simplexmlrpcserver for
>     this,
>     >     but I'm
>     >      >wondering if I should perhaps use twisted instead.
>     >
>     >     I've used xml-rpc to send image data before. It worked.
>     >
>     >    
>     _________________________________________________________________
>     >     Don't just search. Find. Check out the new MSN Search!
>     >     http://search.msn.com/ <http://search.msn.com/>
>     >
>     >     _______________________________________________
>     >     Tutor maillist  -  Tutor at python.org
>     <mailto:Tutor at python.org> <mailto:Tutor at python.org
>     <mailto:Tutor at python.org> >
>     >     http://mail.python.org/mailman/listinfo/tutor
>     >     <http://mail.python.org/mailman/listinfo/tutor
>     <http://mail.python.org/mailman/listinfo/tutor>>
>     >
>     >
>     >
>     >
>     ------------------------------------------------------------------------
>     >
>     > _______________________________________________
>     > Tutor maillist  -   Tutor at python.org <mailto:Tutor at python.org>
>     > http://mail.python.org/mailman/listinfo/tutor
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list