The use and implementation of Asyncore asynchronous module in Python httpclient

Source: Internet
Author: User
Asyncore is an asynchronous socket package, especially the dispatcher class that contains many asynchronous calls to the socket operation method, very sharp, Let's talk about the use of Asyncore asynchronous modules in Python and the implementation of httpclient examples

Basis
This module is the asynchronous implementation of the socket, let's first familiarize ourselves with some of the classes and methods in the module:
1.asyncore.loop

Enter a polling loop until the channel is closed by counting or opening.

2.asyncore.dispatcher

The dispatcher class is a wrapper object for the underlying socket class. To make it more useful, it has a subset of event-handling methods that are called asynchronously. Otherwise, it is a standard non-blocking socket object.
Some advanced events occur when the underlying event tells an asynchronous loop in a particular event or in a particular connection state. For example, we require one socket to connect to another host.

(1) Handle_connect () the first read or write event.
(2) Handle_close () Read event no data available.
(3) Handle_accept read Event Monitor a socket.
(4) Handle_read

Called when the asynchronous loop detects that the channel is calling read ().

(5) Handle_write

Called when the asynchronous loop detects that a writable socket can be written. This approach often achieves buffering performance. Like what

def handle_write (self):  sent = Self.send (self.buffer)  Self.buffer = self.buffer[sent:]

(6) HANDLE_EXPT

When there is a (OOB) data socket connection. This will almost never happen because OOB is finely supported and rarely used.

(7) Handle_connect

Called when the socket creates a connection.

(8) Handle_close

Called when the socket connection is closed.

(9) Handle_error

Called when an exception is thrown and no other processing occurs.

(Ten) Handle_accept

Called when the local listening channel establishes a connection (passive connection) to the remote end.

(one) readable

Called every time the asynchronous loop determines whether to add a channel socket to the Read Event list, which is true by default.

(writable)

Each time the asynchronous loop determines whether to add a channel socket to the Write event list, the default is true.

(Create_socket)

Same as when creating a standard socket.

(+) Connect

The port setting for the standard socket is the same, the first parameter to accept a tuple is the host address, and the second parameter is the port number.

() Send

Sends data to a remote-side socket.

(recv)

Reads up to buffer_size data from the remote-side socket. An empty string means that the channel is closed from the other end.

(+) Listen

Listen for the socket connection.

(+) Bind

Binds the socket to an address.

(+) Accept

Accept a connection that must be bound to a socket and a listening address.

() Close

Close the socket.

3.asyncore.dispatcher_with_send

The dispatcher subclass adds a simple buffered output feature for simple customers and more complex use of asynchat.async_chat.

4.asyncore.file_dispatcher

File_dispatcher requires a file descriptor or file object map as well as an optional parameter, wrapper, using the survey () or loop () function. If you provide a file object or any Fileno () method, the method is called and passed to the File_wrapper constructor. Availability: UNIX.

5.asyncore.file_wrapper

File_wrapper requires an integer file descriptor and calls Os.dup () copy processing, so the original processing may be independent of file_wrapper shutdown. This class implements enough methods to emulate a socket using the File_dispatcher class. Availability: UNIX.

Asyncore instances

1. Implementation of an HTTP client.

Import Socketimport asyncoreclass Client (asyncore.dispatcher):    def __init__ (self, Host, path):    Asyncore.dispatcher.__init__ (self)    self.create_socket (socket.af_inet, socket. SOCK_STREAM)    Self.connect ((host))    Self.buffer = ' GET%s http/1.0\r\n\r\n '% path  def handle_connect ( Self):    pass  def handle_close (self):    self.close ()  def handle_read (self):    print Self.recv (8192 )  def writable (self):    return (len (Self.buffer) > 0)  def handle_write (self):    sent = Self.send ( Self.buffer)    Self.buffer = self.buffer[sent:]client = Client (' www.python.org ', '/') Asyncore.loop ()

Server accepts connections and assigns tasks

Import Socketimport asyncoreclass Echohandler (asyncore.dispatcher_with_send):    def handle_read (self):    data = SELF.RECV (8192)    if data:      self.send (data) class Echoserver (Asyncore.dispatcher):    def __init__ (self, host , port):    asyncore.dispatcher.__init__ (self)    self.create_socket (socket.af_inet, socket. SOCK_STREAM)    self.set_reuse_add ()    Self.bind ((host, port))    Self.listen (5)  def handle_accept (self ):    pair = self.accept ()    If pair is not None:      sock, addr = pair      print ' Incoming connection from%s '% r EPR (addr)      handler = Echohandler (sock) server = Echoserver (' localhost ', 8080) asyncore.loop ()

2. Port mapping with Asyncore (port forwarding)

Import Socket,asyncoreclass Forwarder (asyncore.dispatcher): Def __init__ (self, IP, port, remoteip,remoteport,backlog= 5): asyncore.dispatcher.__init__ (self) self.remoteip=remoteip self.remoteport=remoteport Self.create_socket (so Cket.af_inet,socket. SOCK_STREAM) self.set_reuse_addr () Self.bind ((Ip,port)) Self.listen (backlog) def handle_accept (self): conn, a DDR = Self.accept () # print '---Connect---' sender (Receiver (conn), Self.remoteip,self.remoteport) class receiver (as    Yncore.dispatcher): Def __init__ (self,conn): asyncore.dispatcher.__init__ (self,conn) self.from_remote_buffer= " Self.to_remote_buffer= "Self.sender=none def handle_connect (self): Pass def handle_read (self): Read = Self.rec V (4096) # print '%04i--'%len (read) Self.from_remote_buffer + = Read Def writable (self): return (Len (self.to_    Remote_buffer) > 0) def handle_write (self): sent = Self.send (self.to_remote_buffer) # print '%04i <--'%sent self.tO_remote_buffer = self.to_remote_buffer[sent:] def handle_close (self): Self.close () if Self.sender:self.sende R.close () class sender (Asyncore.dispatcher): Def __init__ (self, receiver, Remoteaddr,remoteport): Asyncore.dispatcher . __init__ (self) self.receiver=receiver receiver.sender=self self.create_socket (socket.af_inet, socket. Sock_stream) Self.connect ((remoteaddr, RemotePort)) def handle_connect (self): Pass def handle_read (self): Read = Self.recv (4096) # print ' <--%04i '%len (read) Self.receiver.to_remote_buffer + = Read Def writable (self): RET Urn (len (self.receiver.from_remote_buffer) > 0) def handle_write (self): sent = Self.send (self.receiver.from_remote_ Buffer) # print '--%04i '%sent self.receiver.from_remote_buffer = self.receiver.from_remote_buffer[sent:] def H Andle_close (self): Self.close () self.receiver.close () if __name__== ' __main__ ': import optparse parser = Optparse. Optionparser () parser.add_option ('-L ', '--local-ip ', dest= ' local_ip ', default= ' 127.0.0.1 ', help= ' local IP address to bind to ') parser.add_option ('- P ', '--local-port ', type= ' int ', dest= ' Local_port ', default=80, help= ' local port to bind to ') parser.add_option ('-R ', '--remote-ip ', dest= ' remote_ip ', help= ' Local IP address to bind to ') parser.add_option ('-P ', '--remote-port ', t ype= ' int ', dest= ' Remote_port ', default=80, help= ' remote port to bind to ') options, args = Parser.parse_args () forwarder (Options.local_ip,options.local_port,options.remote_ip,options.remote_port) Asyncore.loop ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.