本文实例讲述了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>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1298">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680203955338.png" alt="法语写作助手">
</a>
<div class="aritcle_card_info">
<a href="/ai/1298">法语写作助手</a>
<p>法语助手旗下的AI智能写作平台,支持语法、拼写自动纠错,一键改写、润色你的法语作文。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="法语写作助手">
<span>31</span>
</div>
</div>
<a href="/ai/1298" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="法语写作助手">
</a>
</div>
<hr>
<address>Apache/2.2.16 (Debian) Server at dinsdale.python.org Port 80</address>
</body></html>
希望本文所述对大家的Python程序设计有所帮助。
立即学习“Python免费学习笔记(深入)”;
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号