最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文實(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>

希望本文所述對大家的Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論

昌吉市| 临湘市| 文登市| 双辽市| 黔南| 东兰县| 扶余县| 许昌市| 呼图壁县| 惠水县| 乌拉特中旗| 宣恩县| 赤峰市| 荆门市| 南木林县| 射阳县| 城固县| 西平县| 靖西县| 航空| 隆林| 汉阴县| 库伦旗| 吴桥县| 阿坝县| 卫辉市| 苍梧县| 台南市| 龙游县| 荣成市| 怀柔区| 宜兰县| 常德市| 启东市| 东源县| 安阳县| 太保市| 公安县| 天水市| 太和县| 苏尼特左旗|