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


Groups > comp.lang.python > #5179

Re: urllib2 request with binary file as payload

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <lis01361@izone.net.au>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'parameter': 0.05; 'string,': 0.05; 'abstraction': 0.07; 'read-only': 0.07; 'specifying': 0.07; 'stuff,': 0.07; 'python': 0.07; '"""': 0.09; 'curious.': 0.09; 'lambda:': 0.09; 'url.': 0.09; 'api': 0.11; 'written': 0.12; 'server,': 0.12; 'am,': 0.14; 'wrote:': 0.14; "'host':": 0.16; "'rb')": 0.16; '2.6,': 0.16; '2.6:': 0.16; 'docs.': 0.16; 'received:203.24': 0.16; 'send:': 0.16; 'argument': 0.16; 'subject:file': 0.16; 'traceback': 0.16; '(most': 0.16; 'fine': 0.18; 'mapping': 0.19; 'seems': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'thu,': 0.22; '(and': 0.22; 'last):': 0.23; 'worked': 0.24; 'implementing': 0.25; 'script': 0.26; "i'm": 0.26; 'instead': 0.26; 'function': 0.27; "doesn't": 0.28; 'string': 0.29; 'server': 0.29; 'class': 0.29; 'provided.': 0.29; 'buffer,': 0.31; 'quoting': 0.31; 'typeerror:': 0.31; 'videos.': 0.31; 'however,': 0.31; "can't": 0.31; 'data,': 0.31; 'format.': 0.31; "skip:' 10": 0.32; 'to:addr:python-list': 0.32; 'uses': 0.34; 'post': 0.34; 'file.': 0.34; 'file': 0.35; 'header :User-Agent:1': 0.35; '2.6': 0.35; 'none': 0.36; 'data': 0.37; 'skip:o 20': 0.37; 'should': 0.37; 'subject:with': 0.37; '2.5': 0.38; 'http': 0.38; 'sequence': 0.38; 'but': 0.38; 'pretty': 0.38; 'docs': 0.39; 'to:addr:python.org': 0.39; 'works': 0.40; 'takes': 0.40; 'containing': 0.40; 'requests': 0.40; "it's": 0.40; 'allows': 0.40; '2011': 0.62; 'reply-to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72; 'vital': 0.77; '10:20': 0.84; '2.7.1': 0.84; 'expects': 0.84; 'opener': 0.84
In-Reply-To <BANLkTikW_Yq9XL=rBxnsOAYjSjXXVk9MUA@mail.gmail.com>
References <BANLkTikW_Yq9XL=rBxnsOAYjSjXXVk9MUA@mail.gmail.com>
Date Thu, 12 May 2011 10:57:31 +1000
Subject Re: urllib2 request with binary file as payload
From "John Machin" <sjmachin@lexicon.net>
To python-list@python.org
User-Agent SquirrelMail/1.4.21
MIME-Version 1.0
Content-Type text/plain;charset=iso-8859-1
Content-Transfer-Encoding 8bit
X-Priority 3 (Normal)
Importance Normal
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To sjmachin@lexicon.net
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1437.1305161857.9059.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 82.94.164.166
X-Trace 1305161857 news.xs4all.nl 65870 [::ffff:82.94.164.166]:59384
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5179

Show key headers only | View raw


On Thu, May 12, 2011 10:20 am, Michiel Sikma wrote:
> 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):
[snip]
> TypeError: sendall() argument 1 must be string or read-only buffer, not
> file

I don't use this stuff, just curious. But I can read docs. Quoting from
the 2.6.6 docs:

"""
class urllib2.Request(url[, data][, headers][, origin_req_host][,
unverifiable])
This class is an abstraction of a URL request.

url should be a string containing a valid URL.

data may be a string specifying additional data to send to the server, or
None if no such data is needed. Currently HTTP requests are the only ones
that use data; the HTTP request will be a POST instead of a GET when the
data parameter is provided. data should be a buffer in the standard
application/x-www-form-urlencoded format. The urllib.urlencode() function
takes a mapping or sequence of 2-tuples and returns a string in this
format.
"""

2.6 is expecting a string, according to the above. No mention of file.
Moreover it expects the data to be urlencoded. 2.7.1 docs say the same
thing. Are you sure you have shown the code that worked with 2.6?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: urllib2 request with binary file as payload "John Machin" <sjmachin@lexicon.net> - 2011-05-12 10:57 +1000

csiph-web