Python异步通信模块asynchat
来源:互联网 发布:js防水涂料怎么用 编辑:程序博客网 时间:2024/06/07 05:10
Python异步通信模块asynchat
介绍
文档上说:这个模块建立在 asyncore 基础结构上,简化了异步客户端和服务器,使得处理带有以任意字符串终止或者可变长度的元素的协议更加容易。asynchat 定义了你子类化的抽象类 async_chat ,提供了 collect_incoming_data() 和 found_terminator() 方法的实现。它使用了和 asyncore一样的异步循环,和两种类型的通道, asyncore.dispatcher 和 asynchat.async_chat,它们可以被自由地混合在通道映射中。当接收传入的连接请求时,一个 asyncore.dispatcher 服务器通道往往会产生新的 asynchat.async_chat 通道对象。
模块主要包括:
- asynchat.async_chat类 - 这个类是asyncore.dispatcher的抽象子类。一般使用其collect_incoming_data()和found_terminator()方法。
- collect_incoming_data() - 接收数据。
- found_terminator() - 当输入数据流符合由 set_terminator() 设置的终止条件时被调用。
- set_terminator() - 设置终止条件。
- push() - 向通道压入数据以确保其传输。
例子
下面看一个简单的例子来实现一个简单的问候功能
- 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
- 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
HTTPChannel- asynchat.async_chat类的子类。
- 构造函数中首先设置了初始终止字符串为’\r\n’。用来接收http请求。
- collect_incoming_data() - 接收数据并保存。
- found_terminator() - 处理数据。比如请求“http://localhost:8888/kongxx”,当第一次接受到数据时,数据内容类似“GET /kongxx HTTP/1.1”,此时会将其中的kongxx当作用户信息保留。并设置响应文本为“Hello kongxx!”,然后设置终止字符串为’\r\n\r\n’。此时当继续有新数据接收到,这里是http的header信息,直到碰到http到header的结束’\r\n\r\n’时,触发响应结果逻辑,然后程序push了响应头和内容。
HTTPServer - 用了启动服务来监听端口。
测试
运行上面的程序,然后在浏览器地址栏输入http://localhost:8888/kongxx,就会得到下面的响应结果。
- 1
- 1
转载请以链接形式标明本文地址
本文地址:http://blog.csdn.net/kongxx/article/details/51158852
0 0
- Python异步通信模块asynchat
- Python异步通信模块asynchat
- Python异步通信模块asynchat
- Python异步通信模块asyncore
- Python异步通信模块asyncore
- python通信模块
- python中通信模块
- msp430串行通信模块(异步模式)
- 430之通用异步串口通信模块
- python 多进程通信模块
- python的异步IO模块asyncio学习
- python异步并发模块concurrent.futures简析
- python使用select实现异步通信
- Python使用select实现异步通信
- python串口通信模块——pySerial
- python 多进程通信模块实例 代码
- 异步通信
- 异步通信
- java面试①整体流程
- poj2007 Scrambled Polygon【极角排序】
- Android性能优化之UncaughtExceptionHandler定制自己的错误日志系统
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- 数组中使用strcpy、memcpy复制字符串,末尾添加‘\0’
- Python异步通信模块asynchat
- maven+eclipse+SSM框架
- 集合相等问题
- SQL Server中建立外键的方法
- DotNet:Socket Server 异步套接字服务端实现
- 自动化测试之python--selenium的环境准备
- Drools入门学习
- 内核态 Socket TCP编程
- TCP(1)---三次握手及四次挥手