Python中asyncore的用法实例

 更新时间:2014年09月29日 09:27:42   投稿:shichen2014  
这篇文章主要介绍了Python中asyncore的用法,asyncore提供了方便的网络操作方法,本文以连接并解析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() 
   

结果文件的内容为:

<!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 href="http://www.python.org">here</a>.</p>
<hr>
<address>Apache/2.2.16 (Debian) Server at dinsdale.python.org Port 80</address>
</body></html>

希望本文所述对大家的Python程序设计有所帮助。

相关文章

  • 详解Python中list[::-1]的几种用法

    详解Python中list[::-1]的几种用法

    这篇文章主要介绍了详解Python中list[::-1]的几种用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 在pycharm中创建django项目的示例代码

    在pycharm中创建django项目的示例代码

    这篇文章主要介绍了在pycharm中创建django项目的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Python队列、进程间通信、线程案例

    Python队列、进程间通信、线程案例

    这篇文章主要介绍了Python队列、进程间通信、线程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • python实现飞行棋游戏

    python实现飞行棋游戏

    这篇文章主要为大家详细介绍了python实现飞行棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • Python对接六大主流数据库(只需三步)

    Python对接六大主流数据库(只需三步)

    这篇文章主要介绍了Python对接六大主流数据库(只需三步),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • python在windows调用svn-pysvn的实现

    python在windows调用svn-pysvn的实现

    本文主要介绍了python在windows调用svn-pysvn的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • python检测是文件还是目录的方法

    python检测是文件还是目录的方法

    这篇文章主要介绍了python检测是文件还是目录的方法,涉及Python针对文件及目录的检测技巧,需要的朋友可以参考下
    2015-07-07
  • python使用Plotly绘图工具绘制散点图、线形图

    python使用Plotly绘图工具绘制散点图、线形图

    这篇文章主要为大家详细介绍了python使用Plotly绘图工具绘制散点图、线形图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • Python实现TXT数据转三维矩阵

    Python实现TXT数据转三维矩阵

    在数据处理和分析中,将文本文件中的数据转换为三维矩阵是一个常见的任务,本文将详细介绍如何使用Python实现这一任务,感兴趣的小伙伴可以了解下
    2024-01-01
  • 简介二分查找算法与相关的Python实现示例

    简介二分查找算法与相关的Python实现示例

    这篇文章主要介绍了二分查找算法与相关的Python实现示例,Binary Search同时也是算法学习当中最基础的知识,需要的朋友可以参考下
    2015-08-08

最新评论