How to write program to do file transfer based on based omniORBpy - python

I'm now writing a Corba project to do file transfering between client and server.
But I face trouble when I want to upload file from the client to the server.
The IDL I defined is:
interface SecretMessage
{
string send_file(in string file_name, in string file_obj);
};
And I implemented the uploading function in the client code:
f = open('SB.docx', 'rb')
data = ''
for piece in read_in_chunks(f):
data += piece
result = mo.send_file('2.docx', data)
If the file is a plain txt file, there is no problem.
But if the file is a, like jpg, doc, or others except txt, then it does work.
It gives me the error:
omniORB.CORBA.BAD_PARAM: CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType, CORBA.COMPLETED_NO)
Where is the problem?

I think it is because by default omniORB wants to see ASCII data for strings. Try changing your IDL to this
interface SecretMessage
{
typedef sequence<octet> OctetSequence;
string send_file(in string file_name, in OctetSequence file_obj);
};
You can keep your Python client code the same because in the IDL to Python mapping, octet sequences map to Python strings.

Related

Python read from file being written to in a different language

I know this is kind of a dumb situation, but I'm writing to a file in C# and need to be able to read from it in python. I was looking it up, and people were mostly saying to flush the text file, but that only works for python-python, and I need it to work for C#-python. If I read from the file in python and then write to it in C#, everything works fine, but the information is wrong. If I write then read, the correct information is written to the txt file, but then python generates a FileNotFound exception. I am flushing and closing the file in C#, but it still isn't working.
My C# code is as follows:
File.WriteAllText("..\\Ref.txt", "");
System.IO.FileStream s = new System.IO.FileStream("..\\Ref.txt", System.IO.FileMode.OpenOrCreate);
string text = intPtr.ToString();
Byte[] bytes = Encoding.ASCII.GetBytes(text);
s.Write(bytes, 0, bytes.Length);
s.Flush();
Console.WriteLine("Flushed");
s.Close();
And my python code looks like
os.chdir(aDir)
os.system("dotnet run")
r = open("Ref.txt", "r")
ref = r.read()
r.close()
This order generates and exception, but if it were somehow able to not generate an exception, the correct information would be attained from the text file. If I open the text file then run dotnet run, no exception is generated, but I have outdated information (i.e. the info from the last time I ran the program since that was the last time the txt was written to)

How I do convert a Julia stream to "byte content" for a Python package?

I maintain OdsIo.jl, a Julia package that wraps, for ods files specifically, ezodf, a Python package for various OpenDocument formats,
My "ods_readall" function starts by calling ezodf.opendoc.
ezodf.opendoc supports as input, other than the filename, the file byte content itself:
ezodf.opendoc(filename)¶
Parameters: filename (str) – a filename or the file-content as bytes
Is there a way I can use it with a Julia stream (e.g. a network gzipped file as in this question ) intead of plain files?
This seems to work, at least for HTTP, that is what I was interested to:
using HTTP, Pipe, PyCall
const pyio = PyNULL()
const ezodf = PyNULL()
copy!(pyio, pyimport("io"))
copy!(ezodf, pyimport("ezodf"))
function readstream(istream)
return ezodf.opendoc(pyio.BytesIO(istream))
end
urlData = "https://github.com/sylvaticus/OdsIO.jl/raw/master/test/spreadsheet.ods"
doc = #pipe HTTP.get(urlData).body |> readstream(_)

Read Protocol Buffer Message and print Output from _pb2.py file in Python?

I have multiple questions regarding Protocol Buffers. I have a _pb2.py file compiled from .proto. But then I am lost in terms of how to execute a message from a file and output desired results in Python. So far I have this in my Python code:
import ABC_pb2 as proto
import sys
message GroupManageFirstResponse
print results from this message...but I don't know how or where these results come from.
In ABC_pb2.py, I have a message that I would like to read. There are tons of messages. Following is the message in .proto I'd like to read and print output results via print:
message GroupManageFirstResponse
{
optional ReportData total_data = 1;
repeated ResultGroup results = 2;
optional ADSResponse ads_res = 3;
optional GmCalcStat stat_data = 4;
}
My codebase is in C++ and the output is in binary and text file. This is what I don't understand. How is the data flowing in _pb2.py file? When I compile my C++ code, do I have to do anything with _pb2.py or just execute python because results are stored in binary?
Protocol Buffer is by far the hardest topic I've come across to understand. Almost there, though!

Convert and save string to binary file in Python

I'm using PyOBEX to exchange binary files (e.g. images etc.) between my computer (Windows 7) and my phone (Android). However, when I use get() to get a file from my phone, it arrives on my computer as a str. I tried using the chardet module to find out what encoding to use to decode it and eventually turn it into a binary file, but it returned None. type() says that it's a str.
The code is the following:
import bluetooth
import BTDeviceFinder
import PyOBEX.client
name = "myDevice"
address = BTDeviceFinder.find_by_name(name)
port = BTDeviceFinder.find_port(address)
client = PyOBEX.client.BrowserClient(address, port)
client.connect()
a, b = client.get("pic.jpg")
where a is the header (that comes with a file sent via OBEX) and b is the actual file object. b looks something like this: https://drive.google.com/file/d/0By0ywTLTjb3LaFJaM2hWVEdBakE/view?usp=sharing
The PyOBEX documentation or Python forums say nothing about what encoding is used with get().
Do you know how to turn this string into binary data that can be used with write() and then saved in the original file format (i.e. .jpg)?
In python 2.7 strings represent raw bytes (this changes in python 3)
You simply need to save the data to a binary type file:
with open('file.jpg', 'wb') as handle:
handle.write(data_string)
Here is a link to the python doc on open:
https://docs.python.org/2/library/functions.html#open
Note that the "b" represents binary.
Again, this is assuming Python 2.7

Replacing binary mode file object for urllib2

I have a piece of code that looks like this, and it works as is in local (without using any AppEngine):
bodyParams = { "file" : open( filePath, "rb" ) }
request = urllib2.Request( requestUrl, None, self.buildAuthInfo() )
response = self.getOpener().open(request, bodyParams).read()
I would like to get rid of open, i.e open( filePath, "rb" ) , because in Google AppEngine working with files is prohibited (or really inconvenient).
In order to solve the problem, I get a HTTP POST data of a base64 encoded image file, I decode it. I send the encoded data via cURL, using the following command:
curl -X POST -F image=#encoded http://localhost:8080/image-process
encoded is a base64 encoded jpg.
At this point, I tried two different things: putting the decoded data into a StringIO object, or directly passing and using inside bodyParams = { "file" : DECODEDVALUE}. I would expect either of these to work, but I get a HTTP 500 error from the target server (where I'm doing an external API request). This is how I decode the value:
img = self.request.get('image')
DECODEDVALUE = MyStringIO(base64.b64decode(img))
I believe I have a problem related to encoding and/or binary mode. I believe I have a problem related to encoding and/or binary mode.
How can I get rid of open and the use of file objects, in favor of StringIO, BytesIO, or str objects?
NOTE: just for clarity, not related to the original problem. MyStringIO is a subclass of StringIO.StringIO with __len__ added:
class MyStringIO(StringIO.StringIO):
def __len__(self):
return self.len

Categories

Resources