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类型强制转换long to int的代码
Feb 10 Python
详解Python字符串对象的实现
Dec 24 Python
Python爬虫实现网页信息抓取功能示例【URL与正则模块】
May 18 Python
pandas表连接 索引上的合并方法
Jun 08 Python
对pycharm代码整体左移和右移缩进快捷键的介绍
Jul 16 Python
在Python dataframe中出生日期转化为年龄的实现方法
Oct 20 Python
Python中pymysql 模块的使用详解
Aug 12 Python
tensorflow 查看梯度方式
Feb 04 Python
Python接口自动化判断元素原理解析
Feb 24 Python
解决django框架model中外键不落实到数据库问题
May 20 Python
Pycharm新手使用教程(图文详解)
Sep 17 Python
使用Python制作一盏 3D 花灯喜迎元宵佳节
Feb 26 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
详细介绍:Apache+PHP+MySQL配置攻略
2006/09/05 PHP
php格式化电话号码的方法
2015/04/24 PHP
php中Redis的应用--消息传递
2017/03/28 PHP
详解phpstorm2020最新破解方法
2020/09/17 PHP
js生成随机数之random函数随机示例
2013/12/20 Javascript
jQuery中选择器小问题(新人难免遇到)
2014/03/31 Javascript
原生js实现fadein 和 fadeout淡入淡出效果
2014/06/05 Javascript
JavaScript获取当前日期是星期几的方法
2015/04/06 Javascript
jQuery+Ajax实现无刷新分页
2015/10/30 Javascript
理解javascript函数式编程中的闭包(closure)
2016/03/08 Javascript
微信小程序实现拖拽 image 触摸事件监听的实例
2017/08/17 Javascript
vue .js绑定checkbox并获取、改变选中状态的实例
2018/08/24 Javascript
详解react阻止无效重渲染的多种方式
2018/12/11 Javascript
javascript面向对象三大特征之封装实例详解
2019/07/24 Javascript
弱类型语言javascript开发中的一些坑实例小结【变量、函数、数组、对象、作用域等】
2019/08/07 Javascript
基于VUE的v-charts的曲线显示功能
2019/10/01 Javascript
vue-cli3.X快速创建项目的方法步骤
2019/11/14 Javascript
JavaScript实现飞舞的泡泡效果
2020/02/07 Javascript
python教程之用py2exe将PY文件转成EXE文件
2014/06/12 Python
Python显示进度条的方法
2014/09/20 Python
Python3如何解决字符编码问题详解
2017/04/23 Python
浅谈scrapy 的基本命令介绍
2017/06/13 Python
Python基于回溯法子集树模板实现8皇后问题
2017/09/01 Python
用python的requests第三方模块抓取王者荣耀所有英雄的皮肤实例
2017/12/14 Python
Python遍历numpy数组的实例
2018/04/04 Python
Python3+OpenCV2实现图像的几何变换(平移、镜像、缩放、旋转、仿射)
2019/05/13 Python
PyQt5的安装配置过程,将ui文件转为py文件后显示窗口的实例
2019/06/19 Python
pycharm如何设置官方中文(如何汉化)
2020/12/29 Python
HTML5上传文件显示进度的实现代码
2012/08/30 HTML / CSS
x-ua-compatible content=”IE=7, IE=9″意思理解
2013/07/22 HTML / CSS
一家专门做特卖的网站:唯品会
2016/10/09 全球购物
高级技校毕业生自荐信
2013/11/18 职场文书
运动会开幕式邀请函
2014/01/22 职场文书
企业演讲稿范文大全
2014/05/20 职场文书
片区教研活动总结
2014/07/02 职场文书
个人委托书范文
2015/01/28 职场文书