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

Python中asyncore的用法實(shí)例

 更新時(shí)間:2014年09月29日 09:27:42   投稿:shichen2014  
這篇文章主要介紹了Python中asyncore的用法,asyncore提供了方便的網(wǎng)絡(luò)操作方法,本文以連接并解析www.python.org主頁(yè)為例加以說明,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

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

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

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

    在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-05
  • Python隊(duì)列、進(jìn)程間通信、線程案例

    Python隊(duì)列、進(jìn)程間通信、線程案例

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

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

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

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

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

    python在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-02
  • python檢測(cè)是文件還是目錄的方法

    python檢測(cè)是文件還是目錄的方法

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

    python使用Plotly繪圖工具繪制散點(diǎn)圖、線形圖

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

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

    簡(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

最新評(píng)論