Google Chart API, HTTP POST request format.

Discussion in 'Python' started by Slie, Jan 6, 2011.

  1. Slie

    Slie Guest

    http://code.google.com/apis/chart/docs/post_requests.html

    Google will return a chart in your browser from a URL that you have built. If your URL is bigger then 2K characters it will allow you to submit POST requests.

    They gives examples of HTML, JavaScript, and PHP POST requests. Is there a way I can submit a request with Python? Or possibly submit the HTML, JavaScript or PHP using python?(That was a long shot thought). If I do that I would need to find out what to do with the .PNG it gives me.

    Am I headed in the right direction, is the above paragraph about submitting an HTML form from my program even logical?

    I have read several examples on python post requests but I'm not sure mine needs to be that complicated.


    Thank You,
    Slie, Jan 6, 2011
    #1
    1. Advertising

  2. Slie

    Tim Harig Guest

    On 2011-01-06, Slie <> 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()
    Tim Harig, Jan 6, 2011
    #2
    1. Advertising

  3. Slie

    Chris Rebert Guest

    On Wed, Jan 5, 2011 at 11:21 PM, Garland Fulton <> wrote:
    > On Wed, Jan 5, 2011 at 7:26 PM, Tim Harig <> wrote:
    >>
    >> On 2011-01-06, Slie <> 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
    Chris Rebert, Jan 6, 2011
    #3
  4. Slie

    Tim Harig Guest

    On 2011-01-06, Chris Rebert <> wrote:
    > On Wed, Jan 5, 2011 at 11:21 PM, Garland Fulton <> wrote:
    >> On Wed, Jan 5, 2011 at 7:26 PM, Tim Harig <> wrote:
    >>>    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',


    Sorry I didn't notice this got accidently wrapped when I pasted it.
    >>> 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()

    >>
    >> 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.


    For some reason, posts from from this account don't seem to be making it
    through the gateway to usenet so I am only seeing what Mr. Rebert has
    replied to. If you have asked anything else, I very well may have missed
    it.

    > 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.


    Mr. Rebert already answed your question; but, I will expand on that a
    little bit. One of the great things about the Python language is that it
    uses what is commonly known as "duck typing." That is anything object which
    provides the same attributes as another object can be used as though it is
    actually the second object. It is kind of like an implicit form of
    generics that doesn't require a template or an interface.

    The Python standard library makes extensive use of duck typing for file
    like objects. Any object that provides the proper method attributes can be
    given to functions that expect files even though the object is given might
    not be the traditional concept of a file on the filesystem. It might be a
    stringIO object, a socket file object, or something new that you have
    created that supports the required method attributes.

    The semantics and documentation for file like objects have changed a
    little for python2 vs. python3:

    python2: http://docs.python.org/library/stdtypes.html#file-objects
    python3: http://docs.python.org/py3k/library/io.html#io.IOBase

    but they still basically work the same way. Much of the Python 3
    documentation still refers file objects.
    Tim Harig, Jan 6, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    4,776
    Tor Iver Wilhelmsen
    Aug 29, 2005
  2. James
    Replies:
    3
    Views:
    16,231
    Roedy Green
    Nov 25, 2005
  3. gnewsgroup
    Replies:
    4
    Views:
    2,988
    Kevin Spencer
    Feb 6, 2008
  4. Chris Rebert
    Replies:
    0
    Views:
    544
    Chris Rebert
    Jan 6, 2011
  5. scootiePuff
    Replies:
    4
    Views:
    178
    Rilindo Foster
    Aug 21, 2009
Loading...

Share This Page