Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5178 > unrolled thread

urllib2 request with binary file as payload

Started byMichiel Sikma <michiel@thingmajig.org>
First post2011-05-12 02:20 +0200
Last post2011-05-12 02:20 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  urllib2 request with binary file as payload Michiel Sikma <michiel@thingmajig.org> - 2011-05-12 02:20 +0200

#5178 — urllib2 request with binary file as payload

FromMichiel Sikma <michiel@thingmajig.org>
Date2011-05-12 02:20 +0200
Subjecturllib2 request with binary file as payload
Message-ID<mailman.1436.1305159616.9059.python-list@python.org>
Hi there,
I made a small script implementing a part of Youtube's API that allows
you to upload videos. It's pretty straightforward and uses urllib2.
The script was written for Python 2.6, but the server I'm going to use
it on only has 2.5 (and I can't update it right now, unfortunately).
It seems that one vital thing doesn't work in 2.5's urllib2:

--

data = open(video['filename'], 'rb')

opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(settings['upload_location'], data, {
	'Host': 'uploads.gdata.youtube.com',
	'Content-Type': video['type'],
	'Content-Length': '%d' % os.path.getsize(video['filename'])
})
req.get_method = lambda: 'PUT'
url = opener.open(req)

--

This works just fine on 2.6:
send: <open file 'file.mp4', mode 'rb' at 0x1005db580>
sendIng a read()able

However, on 2.5 it refuses:
Traceback (most recent call last):
  File "./pyup", line 119, in <module>
    main()
  File "./pyup", line 116, in main
    url = opener.open(req)
  File "/usr/lib/python2.5/urllib2.py", line 381, in open
    response = self._open(req, data)
  File "/usr/lib/python2.5/urllib2.py", line 399, in _open
    '_open', req)
  File "/usr/lib/python2.5/urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.5/urllib2.py", line 1107, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.5/urllib2.py", line 1079, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib/python2.5/httplib.py", line 866, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.5/httplib.py", line 892, in _send_request
    self.send(body)
  File "/usr/lib/python2.5/httplib.py", line 711, in send
    self.sock.sendall(str)
  File "<string>", line 1, in sendall
TypeError: sendall() argument 1 must be string or read-only buffer, not file

Is there any other way I can do this? I'm pretty new to Python so I'm
not sure how to proceed at this point.

Thanks!

Michiel Sikma
We Demand HTML

http://wedemandhtml.com/
michiel@wedemandhtml.com

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web