Google Chart API, HTTP POST request format.

S

Slie

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,
 
T

Tim Harig

On 2011-01-06 said:
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.
 
C

Chris Rebert

On 2011-01-06 said:
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()

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
 
T

Tim Harig

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top