Python中asyncore的用法實例
更新時間:2014年09月29日 09:27:42 投稿:shichen2014
這篇文章主要介紹了Python中asyncore的用法,asyncore提供了方便的網(wǎng)絡(luò)操作方法,本文以連接并解析www.python.org主頁為例加以說明,需要的朋友可以參考下
本文實例講述了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>
希望本文所述對大家的Python程序設(shè)計有所幫助。
您可能感興趣的文章:
- Python的Asyncore異步Socket模塊及實現(xiàn)端口轉(zhuǎn)發(fā)的例子
- Python學(xué)習之a(chǎn)syncore模塊用法實例教程
- python實現(xiàn)異步回調(diào)機制代碼分享
- 在Python中使用異步Socket編程性能測試
- python異步任務(wù)隊列示例
- 簡單介紹Python的Tornado框架中的協(xié)程異步實現(xiàn)原理
- 跨平臺python異步回調(diào)機制實現(xiàn)和使用方法
- python高并發(fā)異步服務(wù)器核心庫forkcore使用方法
- Python多線程、異步+多進程爬蟲實現(xiàn)代碼
- Python中asyncore異步模塊的用法及實現(xiàn)httpclient的實例
相關(guān)文章
在pycharm中創(chuàng)建django項目的示例代碼
這篇文章主要介紹了在pycharm中創(chuàng)建django項目的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-05-05Python對接六大主流數(shù)據(jù)庫(只需三步)
這篇文章主要介紹了Python對接六大主流數(shù)據(jù)庫(只需三步),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-07-07python在windows調(diào)用svn-pysvn的實現(xiàn)
本文主要介紹了python在windows調(diào)用svn-pysvn的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-02-02Python實現(xiàn)TXT數(shù)據(jù)轉(zhuǎn)三維矩陣
在數(shù)據(jù)處理和分析中,將文本文件中的數(shù)據(jù)轉(zhuǎn)換為三維矩陣是一個常見的任務(wù),本文將詳細介紹如何使用Python實現(xiàn)這一任務(wù),感興趣的小伙伴可以了解下2024-01-01簡介二分查找算法與相關(guān)的Python實現(xiàn)示例
這篇文章主要介紹了二分查找算法與相關(guān)的Python實現(xiàn)示例,Binary Search同時也是算法學(xué)習當中最基礎(chǔ)的知識,需要的朋友可以參考下2015-08-08