By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,855 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,855 IT Pros & Developers. It's quick & easy.

uploading/streaming a file to a java servlet

P: n/a
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/se...rd&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.FileStorageServ let"
params = urllib.urlencode({'TransactionName':'AddDictationF ile','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
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Vasil Slavov wrote:
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/se...rd&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/servlet/com.cerner.capstone.dictation.FileStorageServlet?T ransactionName=AddDictationFile&FileName=myfile.wa v&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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.