欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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è)計有所幫助。

相關(guān)文章

  • 詳解Python中l(wèi)ist[::-1]的幾種用法

    詳解Python中l(wèi)ist[::-1]的幾種用法

    這篇文章主要介紹了詳解Python中l(wèi)ist[::-1]的幾種用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2020-11-11
  • 在pycharm中創(chuàng)建django項目的示例代碼

    在pycharm中創(chuàng)建django項目的示例代碼

    這篇文章主要介紹了在pycharm中創(chuàng)建django項目的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2020-05-05
  • Python隊列、進程間通信、線程案例

    Python隊列、進程間通信、線程案例

    這篇文章主要介紹了Python隊列、進程間通信、線程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2019-10-10
  • python實現(xiàn)飛行棋游戲

    python實現(xiàn)飛行棋游戲

    這篇文章主要為大家詳細介紹了python實現(xiàn)飛行棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Python對接六大主流數(shù)據(jù)庫(只需三步)

    Python對接六大主流數(shù)據(jù)庫(只需三步)

    這篇文章主要介紹了Python對接六大主流數(shù)據(jù)庫(只需三步),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2019-07-07
  • python在windows調(diào)用svn-pysvn的實現(xiàn)

    python在windows調(diào)用svn-pysvn的實現(xiàn)

    本文主要介紹了python在windows調(diào)用svn-pysvn的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2023-02-02
  • python檢測是文件還是目錄的方法

    python檢測是文件還是目錄的方法

    這篇文章主要介紹了python檢測是文件還是目錄的方法,涉及Python針對文件及目錄的檢測技巧,需要的朋友可以參考下
    2015-07-07
  • python使用Plotly繪圖工具繪制散點圖、線形圖

    python使用Plotly繪圖工具繪制散點圖、線形圖

    這篇文章主要為大家詳細介紹了python使用Plotly繪圖工具繪制散點圖、線形圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python實現(xiàn)TXT數(shù)據(jù)轉(zhuǎn)三維矩陣

    Python實現(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)示例

    這篇文章主要介紹了二分查找算法與相關(guān)的Python實現(xiàn)示例,Binary Search同時也是算法學(xué)習當中最基礎(chǔ)的知識,需要的朋友可以參考下
    2015-08-08

最新評論