本文实例讲述了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="/xiazai/code/8843">
<img src="https://img.php.cn/upload/webcode/000/000/010/175696560776768.jpg" alt="aspx1财付通支付接口源码">
</a>
<div class="aritcle_card_info">
<a href="/xiazai/code/8843">aspx1财付通支付接口源码</a>
<p>本支付接口的特点,主要是用xml文件来记录订单详情和支付详情。代码比较简单,只要将里面的商户号、商户key换成你自己的,将回调url换成你的网站,就可以使用了。通过这个实例也可以很好的了解一般在线支付接口的基本工作原理。其中的pay.config文件记录的是支付详情,order.config是订单详情</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="aspx1财付通支付接口源码">
<span>0</span>
</div>
</div>
<a href="/xiazai/code/8843" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="aspx1财付通支付接口源码">
</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号