GNU Info

Info Node: (python2.1-lib.info)Examples 3

(python2.1-lib.info)Examples 3


Prev: HTTP Objects Up: httplib
Enter node , (file) or (file)node

Examples
--------

Here is an example session that uses the `GET' method:

     >>> import httplib
     >>> h = httplib.HTTP('www.cwi.nl')
     >>> h.putrequest('GET', '/index.html')
     >>> h.putheader('Accept', 'text/html')
     >>> h.putheader('Accept', 'text/plain')
     >>> h.endheaders()
     >>> errcode, errmsg, headers = h.getreply()
     >>> print errcode # Should be 200
     >>> f = h.getfile()
     >>> data = f.read() # Get the raw HTML
     >>> f.close()

Here is an example session that shows how to `POST' requests:

     >>> import httplib, urllib
     >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
     >>> h = httplib.HTTP("www.musi-cal.com:80")
     >>> h.putrequest("POST", "/cgi-bin/query")
     >>> h.putheader("Content-type", "application/x-www-form-urlencoded")
     >>> h.putheader("Content-length", "%d" % len(params))
     >>> h.putheader('Accept', 'text/plain')
     >>> h.putheader('Host', 'www.musi-cal.com')
     >>> h.endheaders()
     >>> h.send(params)
     >>> reply, msg, hdrs = h.getreply()
     >>> print reply # should be 200
     >>> data = h.getfile().read() # get the raw HTML


automatically generated by info2www version 1.2.2.9