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/MillenniumMobil e/servlet/"
servlet = "com.cerner.cap stone.dictation .FileStorageSer vlet"
params = urllib.urlencod e({'Transaction Name':'AddDicta tionFile','File Name':'myfakefi le.wav'})
request = urllib2.Request ("".join([url, servlet]), params)
request.add_hea der('User-Agent', 'Velositer')
request.add_hea der('Cookie', sessid)
opener = urllib2.build_o pener()
datastream = opener.open(req uest)
# I am using mod_python
req.write(datas tream.read())
I tried urllib2_file.py :
http://fabien.seisen.o rg/python/urllib2_multipa rt.html
with which you can do stuff like:
data = [('TransactionNa me','AddDictati onFile'),('File Name','back9.jp g'),('file',ope n(file,'rb'))]
request = urllib2.Request ("".join([url, servlet]), data, headers)
response = urllib2.urlopen (request).read( )
and this:
http://aspn.activestat e.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