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