Python中asyncore的用法实例

yipeiwu_com5年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

Python3 虚拟开发环境搭建过程(图文详解)

Python3 虚拟开发环境搭建过程(图文详解)

虚拟环境的搭建 为什么要使用虚拟环境# 1、使不同应用开发环境相互独立 2、环境升级不影响其他应用,也不会影响全局的python环境 3、防止出现包管理混乱及包版本冲突 windows平...

python中的不可变数据类型与可变数据类型详解

前言 python与C/C++不一样,它的变量使用有自己的特点,当初学python的时候,一定要记住“一切皆为对象,一切皆为对象的引用”这句话,其实这个特点类似于JAVA,所以在pyth...

跟老齐学Python之不要红头文件(2)

文件的属性 所谓属性,就是能够通过一个文件对象得到的东西。 复制代码 代码如下: >>> f = open("131.txt","a") >>> f....

解决pyqt中ui编译成窗体.py中文乱码的问题

我在Eric工具下编译的 解决办法: 1、打开 C:\Python27\Lib\site-packages\eric4\i18n,将中文资源包的名称"GB2312."去掉,变成eric4...

python获取本机外网ip的方法

本文实例讲述了python获取本机外网ip的方法。分享给大家供大家参考。具体如下: python从显示ip地址的网站获取本机外网ip,这段python代码抓取网站上的ip地址信息 i...