jcgregorio | 26c0cd7 | 2006-07-03 17:36:17 +0000 | [diff] [blame^] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 | <link rel="first" href="ref.html" title='The httplib2 Library' /> |
| 6 | <link rel='contents' href='contents.html' title="Contents" /> |
| 7 | <link rel='last' href='about.html' title='About this document...' /> |
| 8 | <link rel='help' href='about.html' title='About this document...' /> |
| 9 | <link rel="prev" href="response-objects.html" /> |
| 10 | <link rel="parent" href="module-httplib2.html" /> |
| 11 | <link rel="next" href="about.html" /> |
| 12 | <meta name='aesop' content='information' /> |
| 13 | <title>1.1.4 Examples </title> |
| 14 | </head> |
| 15 | <body> |
| 16 | <DIV CLASS="navigation"> |
| 17 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 18 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 19 | <tr> |
| 20 | <td class='online-navigation'><a rel="prev" title="1.1.3 Response Objects" |
| 21 | href="response-objects.html"><img src='previous.png' |
| 22 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 23 | <td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive" |
| 24 | href="module-httplib2.html"><img src='up.png' |
| 25 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="next" title="About this document ..." |
| 27 | href="about.html"><img src='next.png' |
| 28 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 29 | <td align="center" width="100%">The httplib2 Library</td> |
| 30 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 31 | href="contents.html"><img src='contents.png' |
| 32 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 33 | <td class='online-navigation'><img src='blank.png' |
| 34 | border='0' height='32' alt='' width='32' /></td> |
| 35 | <td class='online-navigation'><img src='blank.png' |
| 36 | border='0' height='32' alt='' width='32' /></td> |
| 37 | </tr></table> |
| 38 | <div class='online-navigation'> |
| 39 | <b class="navlabel">Previous:</b> |
| 40 | <a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</A> |
| 41 | <b class="navlabel">Up:</b> |
| 42 | <a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</A> |
| 43 | <b class="navlabel">Next:</b> |
| 44 | <a class="sectref" rel="next" href="about.html">About this document ...</A> |
| 45 | </div> |
| 46 | <hr /></div> |
| 47 | </DIV> |
| 48 | <!--End of Navigation Panel--> |
| 49 | |
| 50 | <H2><A NAME="SECTION002140000000000000000"></A><A NAME="httplib2-example"></A> |
| 51 | <BR> |
| 52 | 1.1.4 Examples |
| 53 | </H2> |
| 54 | |
| 55 | <P> |
| 56 | To do a simple <code>GET</code> request just supply the absolute URI |
| 57 | of the resource: |
| 58 | |
| 59 | <P> |
| 60 | <div class="verbatim"><pre> |
| 61 | import httplib2 |
| 62 | h = httplib2.Http() |
| 63 | resp, content = h.request("http://bitworking.org/") |
| 64 | assert resp.status == 200 |
| 65 | assert resp['content-type'] == 'text/html' |
| 66 | </pre></div> |
| 67 | |
| 68 | <P> |
| 69 | Here is more complex example that does a PUT |
| 70 | of some text to a resource that requires authentication. |
| 71 | The Http instance also uses a file cache |
| 72 | in the directory <code>.cache</code>. |
| 73 | |
| 74 | <P> |
| 75 | <div class="verbatim"><pre> |
| 76 | import httplib2 |
| 77 | h = httplib2.Http(".cache") |
| 78 | h.add_credentials('name', 'password') |
| 79 | resp, content = h.request("https://example.org/chap/2", |
| 80 | "PUT", body="This is text", |
| 81 | headers={'content-type':'text/plain'} ) |
| 82 | </pre></div> |
| 83 | |
| 84 | <P> |
| 85 | Here is an example that connects to a server that |
| 86 | supports the Atom Publishing Protocol. |
| 87 | |
| 88 | <P> |
| 89 | <div class="verbatim"><pre> |
| 90 | import httplib2 |
| 91 | h = httplib2.Http() |
| 92 | h.add_credentials(myname, mypasswd) |
| 93 | h.follow_all_redirects = True |
| 94 | headers = {'Content-Type': 'application/atom+xml'} |
| 95 | body = """<?xml version="1.0" ?> |
| 96 | <entry xmlns="http://www.w3.org/2005/Atom"> |
| 97 | <title>Atom-Powered Robots Run Amok</title> |
| 98 | <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> |
| 99 | <updated>2003-12-13T18:30:02Z</updated> |
| 100 | <author><name>John Doe</name></author> |
| 101 | <content>Some text.</content> |
| 102 | </entry> |
| 103 | """ |
| 104 | uri = "http://www.example.com/collection/" |
| 105 | resp, content = h.request(uri, "POST", body=body, headers=headers) |
| 106 | </pre></div> |
| 107 | |
| 108 | <P> |
| 109 | |
| 110 | <P> |
| 111 | <IMG |
| 112 | WIDTH="556" HEIGHT="20" ALIGN="BOTTOM" BORDER="0" |
| 113 | SRC="img1.png" |
| 114 | ALT="\begin{center}\vbox{\input{modref.ind} |
| 115 | }\end{center}"> |
| 116 | <P> |
| 117 | |
| 118 | <P> |
| 119 | |
| 120 | <DIV CLASS="navigation"> |
| 121 | <div class='online-navigation'> |
| 122 | <p></p><hr /> |
| 123 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 124 | <tr> |
| 125 | <td class='online-navigation'><a rel="prev" title="1.1.3 Response Objects" |
| 126 | href="response-objects.html"><img src='previous.png' |
| 127 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 128 | <td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive" |
| 129 | href="module-httplib2.html"><img src='up.png' |
| 130 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 131 | <td class='online-navigation'><a rel="next" title="About this document ..." |
| 132 | href="about.html"><img src='next.png' |
| 133 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 134 | <td align="center" width="100%">The httplib2 Library</td> |
| 135 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 136 | href="contents.html"><img src='contents.png' |
| 137 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 138 | <td class='online-navigation'><img src='blank.png' |
| 139 | border='0' height='32' alt='' width='32' /></td> |
| 140 | <td class='online-navigation'><img src='blank.png' |
| 141 | border='0' height='32' alt='' width='32' /></td> |
| 142 | </tr></table> |
| 143 | <div class='online-navigation'> |
| 144 | <b class="navlabel">Previous:</b> |
| 145 | <a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</A> |
| 146 | <b class="navlabel">Up:</b> |
| 147 | <a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</A> |
| 148 | <b class="navlabel">Next:</b> |
| 149 | <a class="sectref" rel="next" href="about.html">About this document ...</A> |
| 150 | </div> |
| 151 | </div> |
| 152 | <hr /> |
| 153 | <span class="release-info">Release 0.2, documentation updated on July 2, 2006.</span> |
| 154 | </DIV> |
| 155 | <!--End of Navigation Panel--> |
| 156 | |
| 157 | </BODY> |
| 158 | </HTML> |