国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 阿克| 新干县| 九龙城区| 凤山县| 青冈县| 汪清县| 象州县| 马公市| 盘锦市| 鄄城县| 双牌县| 麟游县| 黎平县| 西林县| 五华县| 靖安县| 游戏| 开江县| 鄂托克前旗| 琼结县| 上蔡县| 定兴县| 巨野县| 安顺市| 鸡东县| 永福县| 伊通| 聂荣县| 弥勒县| 读书| 邯郸县| 福建省| 阿合奇县| 正蓝旗| 乌拉特前旗| 瑞金市| 凤翔县| 皮山县| 武安市| 元江| 富顺县|