Python中asyncore的用法实例


Posted in Python onSeptember 29, 2014

本文实例讲述了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 相关文章推荐
按日期打印Python的Tornado框架中的日志的方法
May 02 Python
python 迭代器和iter()函数详解及实例
Mar 21 Python
python基于物品协同过滤算法实现代码
May 31 Python
python ftp 按目录结构上传下载的实现代码
Sep 12 Python
Python3.7 新特性之dataclass装饰器
May 27 Python
python过滤中英文标点符号的实例代码
Jul 15 Python
Pytorch加载部分预训练模型的参数实例
Aug 18 Python
修改Pandas的行或列的名字(重命名)
Dec 18 Python
Python 实现向word(docx)中输出
Feb 13 Python
解决pycharm下pyuic工具使用的问题
Apr 08 Python
无惧面试,带你搞懂python 装饰器
Aug 17 Python
python中翻译功能translate模块实现方法
Dec 17 Python
python提示No module named images的解决方法
Sep 29 #Python
python服务器端收发请求的实现代码
Sep 29 #Python
python利用beautifulSoup实现爬虫
Sep 29 #Python
Python中为feedparser设置超时时间避免堵塞
Sep 28 #Python
跟老齐学Python之从格式化表达式到方法
Sep 28 #Python
跟老齐学Python之print详解
Sep 28 #Python
跟老齐学Python之正规地说一句话
Sep 28 #Python
You might like
PHP5在Apache下的两种模式的安装
2006/09/05 PHP
去除php注释和去除空格函数分享
2014/03/13 PHP
TP5框架实现的数据库备份功能示例
2020/04/05 PHP
boxy基于jquery的弹出层对话框插件扩展应用 弹出层选择器
2010/11/21 Javascript
javascript中的继承实例代码
2011/04/27 Javascript
SeaJS入门教程系列之SeaJS介绍(一)
2014/03/03 Javascript
基于JavaScript实现图片点击弹出窗口而不是保存
2016/02/06 Javascript
javaScript中的原型解析【推荐】
2016/05/05 Javascript
jQuery中$.each()函数的用法引申实例
2016/05/12 Javascript
利用javascript实现的三种图片放大镜效果实例(附源码)
2017/01/23 Javascript
jQuery文字轮播特效
2017/02/12 Javascript
angular中子控制器向父控制器传值的实例
2018/10/08 Javascript
浅谈Vue数据响应
2018/11/05 Javascript
vue将单页面改造成多页面应用的方法
2018/11/25 Javascript
用Vue.js在浏览器中实现裁剪图像功能
2019/06/18 Javascript
vue实现购物车选择功能
2020/01/10 Javascript
python使用rabbitmq实现网络爬虫示例
2014/02/20 Python
Python 模板引擎的注入问题分析
2017/01/01 Python
python的paramiko模块实现远程控制和传输示例
2017/10/13 Python
python根据unicode判断语言类型实例代码
2018/01/17 Python
python3.5安装python3-tk详解
2019/04/26 Python
Python利用matplotlib做图中图及次坐标轴的实例
2019/07/08 Python
python的几种矩阵相乘的公式详解
2019/07/10 Python
python itsdangerous模块的具体使用方法
2020/02/17 Python
python 将视频 通过视频帧转换成时间实例
2020/04/23 Python
如何基于Python爬取隐秘的角落评论
2020/07/02 Python
Python虚拟环境virtualenv创建及使用过程图解
2020/12/08 Python
python基于爬虫+django,打造个性化API接口
2021/01/21 Python
Python 实现劳拉游戏的实例代码(四连环、重力四子棋)
2021/03/03 Python
HTML5组件Canvas实现图像灰度化(步骤+实例效果)
2013/04/22 HTML / CSS
J2EE面试题大全
2016/08/06 面试题
应聘教师求职信范文
2015/03/20 职场文书
被告答辩状范文
2015/05/22 职场文书
Python实现生成bmp图像的方法
2021/06/13 Python
详解Python如何批量采集京东商品数据流程
2022/01/22 Python
Java 超详细讲解ThreadLocal类的使用
2022/04/07 Java/Android