国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中asyncore的用法實例

2020-02-23 05:53:50
字體:
供稿:網(wǎng)友

本文實例講述了python中asyncore模塊的用法,分享給大家供大家參考。具體方法如下:

實例代碼如下:

##asyncore  import asyncore,socket  ######################################################################## class AsyncGet(asyncore.dispatcher):   """   the defined class   """    #----------------------------------------------------------------------   def __init__(self, host):     """Constructor"""     asyncore.dispatcher.__init__(self)     self.host = host     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)     self.connect((host, 80))     self.request = "Get /index.html HTTP/1.0/r/n/r/n"     self.outf = None     print "連接 :", host        def handle_connect(self):     print 'connect:', self.host     pass   def handle_read(self):     if not self.outf:       print '正在連接:',self.host     self.outf = open("%s.txt" % self.host, 'wb')     data = self.recv(8192)     if data:       self.outf.write(data)          pass   def handle_writebale(self):     return len(self.request)             def handle_write(self):     num_sent = self.send(self.request)     pass      def handle_close(self):     asyncore.dispatcher.close(self)     print "socket close in:",self.host     if self.outf:       self.outf.close()     pass    if __name__ == "__main__":   AsyncGet("www.python.org")   asyncore.loop()  import asyncore,socket  ######################################################################## class AsyncGet(asyncore.dispatcher):   """   the defined class   """    #----------------------------------------------------------------------   def __init__(self, host):     """Constructor"""     asyncore.dispatcher.__init__(self)     self.host = host     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)     self.connect((host, 80))     self.request = "Get /index.html HTTP/1.0/r/n/r/n"     self.outf = None     print "連接 :", host        def handle_connect(self):     print 'connect:', self.host     pass   def handle_read(self):     if not self.outf:       print '正在連接:',self.host     self.outf = open("%s.txt" % self.host, 'wb')     data = self.recv(8192)     if data:       self.outf.write(data)          pass   def handle_writebale(self):     return len(self.request)             def handle_write(self):     num_sent = self.send(self.request)     pass      def handle_close(self):     asyncore.dispatcher.close(self)     print "socket close in:",self.host     if self.outf:       self.outf.close()     pass    if __name__ == "__main__":   AsyncGet("www.python.org")   asyncore.loop()    

結(jié)果文件的內(nèi)容為:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>302 Found</title></head><body><h1>Found</h1><p>The document has moved <a >here</a>.</p><hr><address>Apache/2.2.16 (Debian) Server at dinsdale.python.org Port 80</address></body></html>            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 确山县| 揭东县| 綦江县| 长寿区| 灵台县| 定日县| 增城市| 林芝县| 潮州市| 博爱县| 玉溪市| 城市| 准格尔旗| 长汀县| 怀宁县| 利津县| 古浪县| 剑河县| 隆回县| 襄樊市| 抚远县| 安塞县| 隆昌县| 满洲里市| 阳信县| 于田县| 滕州市| 双桥区| 沙雅县| 洛川县| 罗甸县| 平原县| 建昌县| 金山区| 仙居县| 墨玉县| 故城县| 桂东县| 阳山县| 彭阳县| 苏尼特右旗|