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