Python's Custom asynchronous IO client

Source: Internet
Author: User

#!/usr/bin/env python#-*-Coding:utf8-*-#__author: "Skiler Hao"#date:2017/5/16 15:04ImportSelectImportSocketImportPprint"""Custom Asynchronous IO module utilizes a nonblocking socket, does not wait for the connection to succeed, does not wait for the requested select module, to listen for the created socket, whether there is ready to write, ready to read"""classHttpResponse:def __init__(self, response_data): Self.raw_data=Response_data Self.data= str (response_data, encoding='Utf-8') self. Header={} self. GET={} self. BODY={} self.response_info="'self.initialize ()defInitialize (self): Header_data, Body_data= Self.data.split ('\r\n\r\n', 1) self. BODY=Body_data header_list= Header_data.split ('\ r \ n')        #print (header_list)         forHeader_iteminchHeader_list:header_byte= Header_item.split (':', 1)            ifLen (header_byte) = = 2: Self. Header[header_byte[0]]= Header_byte[1]            Else: Self.response_info=Header_bytedef __str__(self):returnSelf.response_infoclassHttpRequest:"""a simple package for HttpRequest that encapsulates Socket,host and callback as an object"""    def __init__(self, SK, host, callback):"""receive Socket,host, callback callback function, encapsulate it as Httrequest object:p Aram SK: A Socket object:p Aram Host: hostname or domain name:p Aram Callback:socket After the end of the callback function"""Self.socket=SK Self.host=host Self.callback=CallbackdefFileno (self):"""Select can directly listen to define a Fileno () and return A, HttpRequest () object can be placed directly in the select directly to rotation, listen: Return:fileno () file descriptor """        returnSelf.socket.fileno ()classasynchttpclient:"""Asynchronous HTTP Client"""    def __init__(self): Self.conn=[] self.connection= []    defadd_request (self, host, callback):"""Pass in a host and callback, automatically encapsulate it HttpRequest objects, and then add them to Self.conn and Self.connection:p Aram Host: Pass over a host, a callback function C Allback:p Aram Callback:: Return:"""SK=Socket.socket () sk.setblocking (0)#create a non-blocking socket        Try: Sk.connect (Host,80))#This socket is connected to the specified host's port 80        exceptBlockingioerror as E:Pass        #encapsulate Socket,host,callback as an objectHttp_request =HttpRequest (SK, Host, callback)#Append objects to conn and connectionself.conn.append (http_request) self.connection.append (http_request)defRun (self):#Interface for task invocation         whileTrue:#listening for socket signals            #Select (rlist,wlist,xlist)            #three lists, select listen three lists respectively, rlist whether there is a read signal, wlist whether to write a signal, xlist whether there is an abnormal signal, and finally a timeout            #once there is a corresponding signal for an object, the current current object is returned to the corresponding locationRlist, wlist, xlist = Select.select (Self.conn, Self.connection, Self.conn, 0.05)            #Once there is a write signal             forWinchwlist:#the first is the server, tell the connection is successful, you can write data inside                #print (W.host, ' Connect successfully, write the data quickly! ')                #in the socket hurriedly write on I want to get your homepageTPL ="get/http/1.0\r\nhost:%s\r\n\r\n"%(W.host,) w.socket.send (bytes (TPL, encoding='Utf-8'))                #Select does not have to listen to the socket if there is a write signal, directly removeSelf.connection.remove (W)#Once there is a reading signal             forRinchrlist:#print (R.host, ' start receiving data ~ ~ ~ ')Rec_data=bytes () whileTrue:#start collecting data until data is not received                    Try: Chunk= R.socket.recv (8096)#accepts 8,096 bytes of data at a timeRec_data + =ChunkexceptBlockingioerror as E: Break                #print (r.host,rec_data)                #execute the callback method in the package to receive the data passedR.callback (Rec_data) r.socket.close ()#The corresponding data is not monitored for read dataSelf.conn.remove (R)ifLen (self.conn) = =0:#if there is no socket to listen for a read signal, you can jump out of the loop and end the task.                 BreakdefF1 (rec_data): Response=HttpResponse (rec_data)Print(response. BODY)defF2 (data):Print('Output to screen')#Define a dictionary list that contains the host name and callback functionUrl_list = [    {'Host':'www.baidu.com','Callback': F1}, {'Host':'cn.bing.com','Callback': F1}, {'Host':'www.cnblogs.com','Callback': F1},]#declares an async asyncrequestclient=asynchttpclient () forIteminchurl_list:requestclient.add_request (item['Host'], item['Callback']) Requestclient.run ()

Python's Custom asynchronous IO client

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.