Google Chart API, HTTP POST request format.

Chris Rebert clp2 at rebertia.com
Thu Jan 6 05:57:24 EST 2011


On Wed, Jan 5, 2011 at 11:21 PM, Garland Fulton <stackslip at gmail.com> wrote:
> On Wed, Jan 5, 2011 at 7:26 PM, Tim Harig <usernet at ilthio.net> wrote:
>>>> On 2011年01月06日, Slie <stackslip at gmail.com> wrote:
>> [reformated to <80 columns per RFC 1855 guidelines]
>> > I have read several examples on python post requests but I'm not sure
>> > mine needs to be that complicated.
>>>> >From the HTML example on the page you posted:
>>>>    <form action='https://chart.googleapis.com/chart' method='POST'>
>>        <input type="hidden" name="cht" value="lc"  />
>>        <input type="hidden" name="chtt" value="This is | my chart"  />
>>        <input type='hidden' name='chs' value='600x200' />
>>        <input type="hidden" name="chxt" value="x,y" />
>>        <input type='hidden' name='chd' value='t:40,20,50,20,100'/>
>>        <input type="submit"  />
>>    </form>
>>>> you can retreive the same chart from Python:
>>>>    Python 3.1.2 (r312:79147, Oct  9 2010, 00:16:06)
>>    [GCC 4.4.4] on linux2
>>    Type "help", "copyright", "credits" or "license" for more information.
>>    >>> import urllib.request, urllib.parse
>>    >>> params = urllib.parse.urlencode({'cht':'lc', 'chtt':'This is | my
>>    >>> chart',
>>    ...         'chs':'600x200', 'chxt':'x,y', 'chd':'t:40,20,50,20,100'})
>>    >>> chart =
>> urllib.request.urlopen('https://chart.googleapis.com/chart',
>>    ...         data = params).read()
>>    >>> chartFile = open("chart.png", 'wb')
>>    >>> chartFile.write(chart)
>>    10782
>>    >>> chartFile.close()
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>> Hope this isn't to stupid,
> For the
> chart = urllib.request.urlopen('https://chart.googleapis.com/chart', data =
> params).read()
> Where would I find information on why and what the ).read() part does.

http://docs.python.org/py3k/library/urllib.request.html#urllib.request.urlopen
Specifically: "This function returns a file-like object" (representing
the stream of data received). Thus, .read() on the file-like object
returns the actual bytes obtained from the given URL.
Cheers,
Chris
--
http://blog.rebertia.com


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /