I am working on Django, python and app engine, Can anyone please tell me hoe to send a pdf file to a url using urllib2,(file is InMemoryUploadedFile). I know there is a question in SOF for sending data using urllib2 with the data being in JSON format..But here I want to send a InMemoryUploadedFile which is a pdf uploaded file from html page. Thanks in advance...
Asked
Active
Viewed 216 times
1 Answers
1
You might want to look at Python: HTTP Post a large file with streaming.
You will need to use mmap for streaming the file in memory, then set it to the request
and set the headers to appropriate mime type i.e application/pdf
before opening the url.
import urllib2
import mmap
# Open the file as a memory mapped string. Looks like a string, but
# actually accesses the file behind the scenes.
f = open('somelargefile.pdf','rb')
mmapped_file_as_string = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
# Do the request
request = urllib2.Request(url, mmapped_file_as_string)
request.add_header("Content-Type", "application/pdf")
response = urllib2.urlopen(request)
#close everything
mmapped_file_as_string.close()
f.close()
Since Google app engine doesn't have mmap, you might want to write the file in request.FILES
to the disk temporarily
#f is the file from request.FILES
def handle_uploaded_file(f):
with open('some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
And then read the file from there directly using standard file operations.
Another option is to use StringIO to write your file to in-memory as a string and then pass it to urlib2.request
. This can be inefficient in a multi-user environment compared to using a stream.

Community
- 1
- 1

Pratik Mandrekar
- 9,362
- 4
- 45
- 65
-
It is part of Python standard library optional operating system. Which python version and operating system are you using. Try import mmap at the python interpretor – Pratik Mandrekar Oct 26 '12 at 10:56
-
It seems like Google app engine doesn't support "mmap" [look at this](http://stackoverflow.com/questions/4608239/using-pygeoip-on-appengine-no-module-named-mmap) – Abdul Rafi Oct 26 '12 at 11:37
-
And one more thing you mentioned in your code as f = open('somelargefile.pdf','rb'), But in my case the file is comming from Html input(file = request.FILES('file')). Here file is InMemoryUploadedFile, if I write f = open(file,'rb') it throws an error ---> InMemoryUploadedFile' object has no attribute 'startswith' – Abdul Rafi Oct 26 '12 at 11:42