uploading/streaming a file to a java servlet

V

Vasil Slavov

I apologize for the long email. I hope somebody will have time to read
it and give some suggestions.

I am working on a school project written in Python (using mod_python)
and I need to upload a file to a java servlet. Here are the high-level
instructions given by the servlet authors (the instructions are geared
towards my C#/VB competition (and I really want to show them how cool
Python is):

- Open the file for reading.
- Open an HTTP connection to the servlet and get the RequestStream object.
- Read bytes from the file and write them to the stream until the
entire file has been read.
- Close the stream.

Here is how the url looks like:
http://10.0.0.21/MillenniumMobile/s...&Username=team1&Password=password&Domain=mobj

I am having a hard time figuring out how to translate the above
instructions into something which can be implemented in Python. How am
I supposed to "stream" the file.

I get a successful XML response with the following code, but
obviously, it doesn't do anything useful because it doesn't actually
upload the file:

url = "http://10.0.0.21/MillenniumMobile/servlet/"
servlet = "com.cerner.capstone.dictation.FileStorageServlet"
params = urllib.urlencode({'TransactionName':'AddDictationFile','FileName':'myfakefile.wav'})
request = urllib2.Request("".join([url, servlet]), params)
request.add_header('User-Agent', 'Velositer')
request.add_header('Cookie', sessid)
opener = urllib2.build_opener()
datastream = opener.open(request)
# I am using mod_python
req.write(datastream.read())

I tried urllib2_file.py:
http://fabien.seisen.org/python/urllib2_multipart.html

with which you can do stuff like:
data = [('TransactionName','AddDictationFile'),('FileName','back9.jpg'),('file',open(file,'rb'))]
request = urllib2.Request("".join([url, servlet]), data, headers)
response = urllib2.urlopen(request).read()

and this:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

but they don't work. I get errors that the servlet can't parse the XML request.

Any suggestions would be greatly appreciated.

Thanks in advance,
Vasil
 
K

Kent Johnson

Vasil said:
I am working on a school project written in Python (using mod_python)
and I need to upload a file to a java servlet. Here are the high-level
instructions given by the servlet authors:

- Open the file for reading.
- Open an HTTP connection to the servlet and get the RequestStream object.
- Read bytes from the file and write them to the stream until the
entire file has been read.
- Close the stream.

Is the request supposed to be a GET or POST or ?? I'll assume POST because sending a data stream on
a GET is just too ugly. Even so IMO this protocol is perverse and you will have to work around it.
You have to avoid putting the request parameters in the request body (which is normal for a POST)
and avoid making the data into another request parameter (which would also be normal).
Here is how the url looks like:
http://10.0.0.21/MillenniumMobile/s...&Username=team1&Password=password&Domain=mobj

I am having a hard time figuring out how to translate the above
instructions into something which can be implemented in Python. How am
I supposed to "stream" the file.

I would try something like this:
url =
'http://10.0.0.21/MillenniumMobile/s...&Username=team1&Password=password&Domain=mobj'
f = open(datafile, 'rb')
data = f.read() # or whatever you need to do to get the actual data into a string
f.close()
req = urllib2.url_open(url, data)
result = req.read()
I get a successful XML response with the following code

Your description of the protocol doesn't say anything about XML, did you leave something out?

Kent
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top