File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -222,29 +222,37 @@ \section{\module{asyncore} ---
222222
223223\subsection {asyncore Example basic HTTP client \label {asyncore-example } }
224224
225- As a basic example, below is a very basic HTTP client that uses the
226- \class {dispatcher} class to implement its socket handling:
225+ Here is a very basic HTTP client that uses the \class {dispatcher}
226+ class to implement its socket handling:
227227
228228\begin {verbatim }
229+ import asyncore, socket
230+
229231class http_client(asyncore.dispatcher):
230- def __init__(self, host,path):
232+
233+ def __init__(self, host, path):
231234 asyncore.dispatcher.__init__(self)
232- self.path = path
233235 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
234236 self.connect( (host, 80) )
235- self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % self. path
236-
237+ self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
238+
237239 def handle_connect(self):
238240 pass
239-
241+
242+ def handle_close(self):
243+ self.close()
244+
240245 def handle_read(self):
241- data = self.recv(8192)
242- print data
243-
246+ print self.recv(8192)
247+
244248 def writable(self):
245249 return (len(self.buffer) > 0)
246-
250+
247251 def handle_write(self):
248252 sent = self.send(self.buffer)
249253 self.buffer = self.buffer[sent:]
254+
255+ c = http_client('www.python.org', '/')
256+
257+ asyncore.loop()
250258\end {verbatim }
You can’t perform that action at this time.
0 commit comments