开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

source-code-analysis/python3.8.1

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.8.1
/
Doc
/
library
/
asynchat.rst
python3.8.1
/
Doc
/
library
/
asynchat.rst
asynchat.rst 8.33 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月16日 09:46 +08:00 . git init

:mod:`asynchat` --- Asynchronous socket command/response handler

.. module:: asynchat
 :synopsis: Support for asynchronous command/response protocols.

.. moduleauthor:: Sam Rushing <rushing@nightmare.com>
.. sectionauthor:: Steve Holden <sholden@holdenweb.com>

Source code: :source:`Lib/asynchat.py`

.. deprecated:: 3.6
 Please use :mod:`asyncio` instead.


Note

This module exists for backwards compatibility only. For new code we recommend using :mod:`asyncio`.

This module builds on the :mod:`asyncore` infrastructure, simplifying asynchronous clients and servers and making it easier to handle protocols whose elements are terminated by arbitrary strings, or are of variable length. :mod:`asynchat` defines the abstract class :class:`async_chat` that you subclass, providing implementations of the :meth:`collect_incoming_data` and :meth:`found_terminator` methods. It uses the same asynchronous loop as :mod:`asyncore`, and the two types of channel, :class:`asyncore.dispatcher` and :class:`asynchat.async_chat`, can freely be mixed in the channel map. Typically an :class:`asyncore.dispatcher` server channel generates new :class:`asynchat.async_chat` channel objects as it receives incoming connection requests.

This class is an abstract subclass of :class:`asyncore.dispatcher`. To make practical use of the code you must subclass :class:`async_chat`, providing meaningful :meth:`collect_incoming_data` and :meth:`found_terminator` methods. The :class:`asyncore.dispatcher` methods can be used, although not all make sense in a message/response context.

Like :class:`asyncore.dispatcher`, :class:`async_chat` defines a set of events that are generated by an analysis of socket conditions after a :c:func:`select` call. Once the polling loop has been started the :class:`async_chat` object's methods are called by the event-processing framework with no action on the part of the programmer.

Two class attributes can be modified, to improve performance, or possibly even to conserve memory.

.. data:: ac_in_buffer_size

 The asynchronous input buffer size (default ``4096``).


.. data:: ac_out_buffer_size

 The asynchronous output buffer size (default ``4096``).

Unlike :class:`asyncore.dispatcher`, :class:`async_chat` allows you to define a :abbr:`FIFO (first-in, first-out)` queue of producers. A producer need have only one method, :meth:`more`, which should return data to be transmitted on the channel. The producer indicates exhaustion (i.e. that it contains no more data) by having its :meth:`more` method return the empty bytes object. At this point the :class:`async_chat` object removes the producer from the queue and starts using the next producer, if any. When the producer queue is empty the :meth:`handle_write` method does nothing. You use the channel object's :meth:`set_terminator` method to describe how to recognize the end of, or an important breakpoint in, an incoming transmission from the remote endpoint.

To build a functioning :class:`async_chat` subclass your input methods :meth:`collect_incoming_data` and :meth:`found_terminator` must handle the data that the channel receives asynchronously. The methods are described below.

.. method:: async_chat.close_when_done()

 Pushes a ``None`` on to the producer queue. When this producer is popped off
 the queue it causes the channel to be closed.


.. method:: async_chat.collect_incoming_data(data)

 Called with *data* holding an arbitrary amount of received data. The
 default method, which must be overridden, raises a
 :exc:`NotImplementedError` exception.


.. method:: async_chat.discard_buffers()

 In emergencies this method will discard any data held in the input and/or
 output buffers and the producer queue.


.. method:: async_chat.found_terminator()

 Called when the incoming data stream matches the termination condition set
 by :meth:`set_terminator`. The default method, which must be overridden,
 raises a :exc:`NotImplementedError` exception. The buffered input data
 should be available via an instance attribute.


.. method:: async_chat.get_terminator()

 Returns the current terminator for the channel.


.. method:: async_chat.push(data)

 Pushes data on to the channel's queue to ensure its transmission.
 This is all you need to do to have the channel write the data out to the
 network, although it is possible to use your own producers in more complex
 schemes to implement encryption and chunking, for example.


.. method:: async_chat.push_with_producer(producer)

 Takes a producer object and adds it to the producer queue associated with
 the channel. When all currently-pushed producers have been exhausted the
 channel will consume this producer's data by calling its :meth:`more`
 method and send the data to the remote endpoint.


.. method:: async_chat.set_terminator(term)

 Sets the terminating condition to be recognized on the channel. ``term``
 may be any of three types of value, corresponding to three different ways
 to handle incoming protocol data.

 +-----------+---------------------------------------------+
 | term | Description |
 +===========+=============================================+
 | *string* | Will call :meth:`found_terminator` when the |
 | | string is found in the input stream |
 +-----------+---------------------------------------------+
 | *integer* | Will call :meth:`found_terminator` when the |
 | | indicated number of characters have been |
 | | received |
 +-----------+---------------------------------------------+
 | ``None`` | The channel continues to collect data |
 | | forever |
 +-----------+---------------------------------------------+

 Note that any data following the terminator will be available for reading
 by the channel after :meth:`found_terminator` is called.


asynchat Example

The following partial example shows how HTTP requests can be read with :class:`async_chat`. A web server might create an :class:`http_request_handler` object for each incoming client connection. Notice that initially the channel terminator is set to match the blank line at the end of the HTTP headers, and a flag indicates that the headers are being read.

Once the headers have been read, if the request is of type POST (indicating that further data are present in the input stream) then the Content-Length: header is used to set a numeric terminator to read the right amount of data from the channel.

The :meth:`handle_request` method is called once all relevant input has been marshalled, after setting the channel terminator to None to ensure that any extraneous data sent by the web client are ignored.

import asynchat

class http_request_handler(asynchat.async_chat):

 def __init__(self, sock, addr, sessions, log):
 asynchat.async_chat.__init__(self, sock=sock)
 self.addr = addr
 self.sessions = sessions
 self.ibuffer = []
 self.obuffer = b""
 self.set_terminator(b"\r\n\r\n")
 self.reading_headers = True
 self.handling = False
 self.cgi_data = None
 self.log = log

 def collect_incoming_data(self, data):
 """Buffer the data"""
 self.ibuffer.append(data)

 def found_terminator(self):
 if self.reading_headers:
 self.reading_headers = False
 self.parse_headers(b"".join(self.ibuffer))
 self.ibuffer = []
 if self.op.upper() == b"POST":
 clen = self.headers.getheader("content-length")
 self.set_terminator(int(clen))
 else:
 self.handling = True
 self.set_terminator(None)
 self.handle_request()
 elif not self.handling:
 self.set_terminator(None) # browsers sometimes over-send
 self.cgi_data = parse(self.headers, b"".join(self.ibuffer))
 self.handling = True
 self.ibuffer = []
 self.handle_request()
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.8.1.git
git@gitee.com:python_sourcecode/python3.8.1.git
python_sourcecode
python3.8.1
python3.8.1
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /