- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python中asyncore异步模块的用法及实现httpclient的实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
基础 这个模块是socket的异步实现,让我们先来熟悉一下模块中的一些类和方法: 1.asyncore.loop 。
输入一个轮询循环直到通过计数或打开的通道已关闭.
2.asyncore.dispatcher 。
dispatcher类是一个底层socket类的包装对象。要使它更有用, 它有一部分事件处理方法被异步循环调用。否则它就是一个标准的非阻塞socket对象。 底层的事件在特定事件或特定的连接状态告诉异步循环,某些高级事件发生了。例如, 我们要求一个socket连接到另一个主机.
(1)handle_connect() 第一次读或写事件。 (2)handle_close() 读事件没有数据可用。 (3)handle_accept 读事件监听一个socket。 (4)handle_read 。
在异步循环察觉到通道呼叫read()时调用.
(5)handle_write 。
在异步循环检测到一个可写的socket可以写的时候调用。这种方法经常实现缓冲性能。比如 。
1
2
3
|
def
handle_write(
self
):
sent
=
self
.send(
self
.
buffer
)
self
.
buffer
=
self
.
buffer
[sent:]
|
(6)handle_expt 。
当有(OOB)数据套接字连接。这几乎永远不会发生,因为OOB精细地支持和很少使用.
(7)handle_connect 。
当socket创建一个连接时调用.
(8)handle_close 。
当socket连接关闭时调用.
(9)handle_error 。
当引发一个异常并没有其他处理时调用.
(10)handle_accept 。
当本地监听通道与远程端建立连接(被动连接)时调用.
(11)readable 。
每次在异步循环确定是否添加一个通道socket到读事件列表时调用,默认都为True.
(12)writable 。
每次在异步循环确定是否添加一个通道socket到写事件列表时调用, 默认为True.
(13)create_socket 。
与创建标准socket的时候相同.
(14)connect 。
与标准socket的端口设置是相同, 接受一个元组第一个参数为主机地址,第二个参数是端口号.
(15)send 。
向远程端socket发送数据.
(16)recv 。
从远程端socket读取最多buffer_size的数据。一个空的字符串意味着从另一端通道已关闭.
(17)listen 。
监听socket连接.
(18)bind 。
将socket绑定到地址.
(19)accept 。
接受一个连接, 必须绑定到一个socket和监听地址.
(20)close 。
关闭socket.
3.asyncore.dispatcher_with_send 。
dispatcher子类添加了简单的缓冲输出功能用于简单的客户,更复杂的使用asynchat.async_chat.
4.asyncore.file_dispatcher 。
file_dispatcher需要一个文件描述符或文件对象地图以及一个可选的参数,包装,使用调查()或循环()函数。如果提供一个文件对象或任何fileno()方法,该方法将调用和传递到file_wrapper构造函数。可用性:UNIX.
5.asyncore.file_wrapper 。
file_wrapper需要一个整数文件描述符并调用os.dup()复制处理,这样原来的处理可能是独立于file_wrapper关闭。这个类实现足够的方法来模拟一个套接字使用file_dispatcher类。可用性:UNIX.
asyncore 实例 。
1.一个http client的实现.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import
socket
import
asyncore
class
Client(asyncore.dispatcher):
def
__init__(
self
, host, path):
asyncore.dispatcher.__init__(
self
)
self
.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self
.connect((host,
80
))
self
.
buffer
=
'GET %s HTTP/1.0\r\n\r\n'
%
path
def
handle_connect(
self
):
pass
def
handle_close(
self
):
self
.close()
def
handle_read(
self
):
print
self
.recv(
8192
)
def
writable(
self
):
return
(
len
(
self
.
buffer
) >
0
)
def
handle_write(
self
):
sent
=
self
.send(
self
.
buffer
)
self
.
buffer
=
self
.
buffer
[sent:]
client
=
Client(
'www.python.org'
,
'/'
)
asyncore.loop()
|
服务器接受连接和分配任务 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import
socket
import
asyncore
class
EchoHandler(asyncore.dispatcher_with_send):
def
handle_read(
self
):
data
=
self
.recv(
8192
)
if
data:
self
.send(data)
class
EchoServer(asyncore.dispatcher):
def
__init__(
self
, host, port):
asyncore.dispatcher.__init__(
self
)
self
.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self
.set_reuse_add()
self
.bind((host, port))
self
.listen(
5
)
def
handle_accept(
self
):
pair
=
self
.accept()
if
pair
is
not
None
:
sock, addr
=
pair
print
'Incoming connection from %s'
%
repr
(addr)
handler
=
EchoHandler(sock)
server
=
EchoServer(
'localhost'
,
8080
)
asyncore.loop()
|
2.利用asyncore的端口映射(端口转发) 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
import
socket,asyncore
class
forwarder(asyncore.dispatcher):
def
__init__(
self
, ip, port, remoteip,remoteport,backlog
=
5
):
asyncore.dispatcher.__init__(
self
)
self
.remoteip
=
remoteip
self
.remoteport
=
remoteport
self
.create_socket(socket.AF_INET,socket.SOCK_STREAM)
self
.set_reuse_addr()
self
.bind((ip,port))
self
.listen(backlog)
def
handle_accept(
self
):
conn, addr
=
self
.accept()
# print '--- Connect --- '
sender(receiver(conn),
self
.remoteip,
self
.remoteport)
class
receiver(asyncore.dispatcher):
def
__init__(
self
,conn):
asyncore.dispatcher.__init__(
self
,conn)
self
.from_remote_buffer
=
''
self
.to_remote_buffer
=
''
self
.sender
=
None
def
handle_connect(
self
):
pass
def
handle_read(
self
):
read
=
self
.recv(
4096
)
# print '%04i -->'%len(read)
self
.from_remote_buffer
+
=
read
def
writable(
self
):
return
(
len
(
self
.to_remote_buffer) >
0
)
def
handle_write(
self
):
sent
=
self
.send(
self
.to_remote_buffer)
# print '%04i <--'%sent
self
.to_remote_buffer
=
self
.to_remote_buffer[sent:]
def
handle_close(
self
):
self
.close()
if
self
.sender:
self
.sender.close()
class
sender(asyncore.dispatcher):
def
__init__(
self
, receiver, remoteaddr,remoteport):
asyncore.dispatcher.__init__(
self
)
self
.receiver
=
receiver
receiver.sender
=
self
self
.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self
.connect((remoteaddr, remoteport))
def
handle_connect(
self
):
pass
def
handle_read(
self
):
read
=
self
.recv(
4096
)
# print '<-- %04i'%len(read)
self
.receiver.to_remote_buffer
+
=
read
def
writable(
self
):
return
(
len
(
self
.receiver.from_remote_buffer) >
0
)
def
handle_write(
self
):
sent
=
self
.send(
self
.receiver.from_remote_buffer)
# print '--> %04i'%sent
self
.receiver.from_remote_buffer
=
self
.receiver.from_remote_buffer[sent:]
def
handle_close(
self
):
self
.close()
self
.receiver.close()
if
__name__
=
=
'__main__'
:
import
optparse
parser
=
optparse.OptionParser()
parser.add_option(
'-l'
,
'--local-ip'
,
dest
=
'local_ip'
,default
=
'127.0.0.1'
,
help
=
'Local IP address to bind to'
)
parser.add_option(
'-p'
,
'--local-port'
,
type
=
'int'
,dest
=
'local_port'
,default
=
80
,
help
=
'Local port to bind to'
)
parser.add_option(
'-r'
,
'--remote-ip'
,dest
=
'remote_ip'
,
help
=
'Local IP address to bind to'
)
parser.add_option(
'-P'
,
'--remote-port'
,
type
=
'int'
,dest
=
'remote_port'
,default
=
80
,
help
=
'Remote port to bind to'
)
options, args
=
parser.parse_args()
forwarder(options.local_ip,options.local_port,options.remote_ip,options.remote_port)
asyncore.loop()
|
最后此篇关于Python中asyncore异步模块的用法及实现httpclient的实例的文章就讲到这里了,如果你想了解更多关于Python中asyncore异步模块的用法及实现httpclient的实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
最近我在用 RestSharp消耗我的 Restful 资源。并期望在服务器和客户端之间与 JSon 交换数据。下面是我的 C# 代码。 var client = new RestSharp.Rest
我正在阅读 Bartosz Milewski 的一篇文章,其中他定义了以下函数: instance Applicative Chan where pure x = Chan (repeat x)
‘…' 其实是go的一种语法糖。 它的第一个用法主要是用于函数有多个不定参数的情况,可以接受多个不确定数量的参数。 第二个用法是slice可以被打散进行传递。 实例:
前言 在算face_track_id map有感: 开始验证 data={"state":[1,1,2,2,1,2,2,2],"pop":[&quo
本文实例讲述了php访问数组最后一个元素的函数end()用法。分享给大家供大家参考。具体分析如下: end()函数在PHP中用于检索数组中的最后一个元素。end()函数需要一个数组作为其唯一参数,
我使用的是 jdk1.8.0_92。我的虚拟机如下所示。 $java -version java version "1.8.0_92" Java(TM) SE Runtime Environment
我的情况是我需要将所有匹配 http://mywebsite.com/portfolio/[anyname] 的请求定向到 http://mywebsite.com/portfolio.php?用户名
我正在尝试在 NLTK 中使用语音标记并使用了以下命令: >>> text = nltk.word_tokenize("And now for something completely differe
#include typedef QList IntList; qRegisterMetaType("IntList"); error C2909: 'qRegisterMetaType':
来自 here我知道 BN_CTX 是一个保存 BIGNUM 临时变量的结构。这些 BIGNUM 变量什么时候会进入 BN_CTX 的 BN_POOL?如果我有一个 bignum_ctx BN_CTX
尝试为 ABPersonRef 创建对象例子:ABpersonRef 引用; 已包含Addressbook和AddressBookUI框架即使这样,当我编译时,它仍显示“ABPersonRef”未声明
我无法使用 GetAltTabInfo。可能是一个愚蠢的错误,但这有什么问题呢? HWND taskSwitcher = FindWindow(L"TaskSwitcherWnd", L"Task S
JSLint4Java 是 JSLint 的 Java 包装器。我需要这样的东西在我的 GWT 项目中使用,但使用 JSLint4Java 的唯一方法似乎是从命令行或通过 ANT 任务。有谁知道是否有
我有一个持久化实体对象的方法 persistData() 。我有另一个方法 findData() ,它对同一实体类执行 find() 操作以获取持久的主键值。当我在实体类的@PostPersist中调
下面是我的代码。请查看。 1. bool isUnavailable = db.Deploys.Where(p => p.HostEnvironmentId == Guid.Parse(h
这个问题已经有答案了: Why can't a Generic Type Parameter have a lower bound in Java? (6 个回答) 已关闭 9 年前。 我试图理解为什
我正在尝试使用 scala 编译器 Y 警告,但我认为我做得不对。在下面的示例中,nums 未使用,因此我希望 -Ywarn-value-discard 打印一个警告。有两个 if 条件,一个嵌套在另
用户被要求从某个给定的集合中选择一个 ID。我检查该 ID 是否存在于我的集合中,如果不存在,我会抛出 IndexOutOfBoundsException 并稍后捕获它。我实际上可以使用该异常来达到这
我正在尝试减少从 OSM 路径数据生成的形状文件。我正在使用 VTS 的 DouglasPeuckerSimplifier 实现。我想为特定 GTFS(通用交通提要规范)构建路线图的 geojson。
我明白了?!是排除某个模式,例如 a(?!b) 表示如果“a”后面没有“b”,它将匹配“a”。我的问题是,假设我有一个包含以下内容的文件: a cat is a cat, a dog is a dog
我是一名优秀的程序员,十分优秀!