| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="prev" href="node571.html" /> |
| 13 | <link rel="parent" href="module-asynchat.html" /> |
| 14 | <link rel="next" href="netdata.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>11.26.2 asynchat Example </title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="11.26.1 asynchat - Auxiliary" |
| 24 | href="node571.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="11.26 asynchat " |
| 27 | href="module-asynchat.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="12. Internet Data Handling" |
| 30 | href="netdata.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Library Reference</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 37 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="node571.html">11.26.1 asynchat - Auxiliary</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="module-asynchat.html">11.26 asynchat </A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="netdata.html">12. Internet Data Handling</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H2><A NAME="SECTION00132620000000000000000"></A><A NAME="asynchat-example"></A> |
| 55 | <BR> |
| 56 | 11.26.2 asynchat Example |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | The following partial example shows how HTTP requests can be read with |
| 61 | <tt class="class">async_chat</tt>. A web server might create an <tt class="class">http_request_handler</tt> object for |
| 62 | each incoming client connection. Notice that initially the |
| 63 | channel terminator is set to match the blank line at the end of the HTTP |
| 64 | headers, and a flag indicates that the headers are being read. |
| 65 | |
| 66 | <P> |
| 67 | Once the headers have been read, if the request is of type POST |
| 68 | (indicating that further data are present in the input stream) then the |
| 69 | <code>Content-Length:</code> header is used to set a numeric terminator to |
| 70 | read the right amount of data from the channel. |
| 71 | |
| 72 | <P> |
| 73 | The <tt class="method">handle_request()</tt> method is called once all relevant input |
| 74 | has been marshalled, after setting the channel terminator to <code>None</code> |
| 75 | to ensure that any extraneous data sent by the web client are ignored. |
| 76 | |
| 77 | <P> |
| 78 | <div class="verbatim"><pre> |
| 79 | class http_request_handler(asynchat.async_chat): |
| 80 | |
| 81 | def __init__(self, conn, addr, sessions, log): |
| 82 | asynchat.async_chat.__init__(self, conn=conn) |
| 83 | self.addr = addr |
| 84 | self.sessions = sessions |
| 85 | self.ibuffer = [] |
| 86 | self.obuffer = "" |
| 87 | self.set_terminator("\r\n\r\n") |
| 88 | self.reading_headers = True |
| 89 | self.handling = False |
| 90 | self.cgi_data = None |
| 91 | self.log = log |
| 92 | |
| 93 | def collect_incoming_data(self, data): |
| 94 | """Buffer the data""" |
| 95 | self.ibuffer.append(data) |
| 96 | |
| 97 | def found_terminator(self): |
| 98 | if self.reading_headers: |
| 99 | self.reading_headers = False |
| 100 | self.parse_headers("".join(self.ibuffer)) |
| 101 | self.ibuffer = [] |
| 102 | if self.op.upper() == "POST": |
| 103 | clen = self.headers.getheader("content-length") |
| 104 | self.set_terminator(int(clen)) |
| 105 | else: |
| 106 | self.handling = True |
| 107 | self.set_terminator(None) |
| 108 | self.handle_request() |
| 109 | elif not self.handling: |
| 110 | self.set_terminator(None) # browsers sometimes over-send |
| 111 | self.cgi_data = parse(self.headers, "".join(self.ibuffer)) |
| 112 | self.handling = True |
| 113 | self.ibuffer = [] |
| 114 | self.handle_request() |
| 115 | </pre></div> |
| 116 | |
| 117 | <P> |
| 118 | |
| 119 | <DIV CLASS="navigation"> |
| 120 | <div class='online-navigation'> |
| 121 | <p></p><hr /> |
| 122 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 123 | <tr> |
| 124 | <td class='online-navigation'><a rel="prev" title="11.26.1 asynchat - Auxiliary" |
| 125 | href="node571.html"><img src='../icons/previous.png' |
| 126 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 127 | <td class='online-navigation'><a rel="parent" title="11.26 asynchat " |
| 128 | href="module-asynchat.html"><img src='../icons/up.png' |
| 129 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 130 | <td class='online-navigation'><a rel="next" title="12. Internet Data Handling" |
| 131 | href="netdata.html"><img src='../icons/next.png' |
| 132 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 133 | <td align="center" width="100%">Python Library Reference</td> |
| 134 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 135 | href="contents.html"><img src='../icons/contents.png' |
| 136 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 137 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 138 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 139 | <td class='online-navigation'><a rel="index" title="Index" |
| 140 | href="genindex.html"><img src='../icons/index.png' |
| 141 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 142 | </tr></table> |
| 143 | <div class='online-navigation'> |
| 144 | <b class="navlabel">Previous:</b> |
| 145 | <a class="sectref" rel="prev" href="node571.html">11.26.1 asynchat - Auxiliary</A> |
| 146 | <b class="navlabel">Up:</b> |
| 147 | <a class="sectref" rel="parent" href="module-asynchat.html">11.26 asynchat </A> |
| 148 | <b class="navlabel">Next:</b> |
| 149 | <a class="sectref" rel="next" href="netdata.html">12. Internet Data Handling</A> |
| 150 | </div> |
| 151 | </div> |
| 152 | <hr /> |
| 153 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 154 | </DIV> |
| 155 | <!--End of Navigation Panel--> |
| 156 | <ADDRESS> |
| 157 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 158 | </ADDRESS> |
| 159 | </BODY> |
| 160 | </HTML> |