Files
hustoj/web/python-docs/library/asyncore.html
2025-03-09 17:10:56 +08:00

534 lines
45 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>asyncore --- 异步套接字处理器 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="asynchat --- 异步套接字指令/响应处理程序" href="asynchat.html" />
<link rel="prev" title="selectors --- 高级 I/O 复用库" href="selectors.html" />
<link rel="canonical" href="https://docs.python.org/3/library/asyncore.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncore</span></code> --- 异步套接字处理器</a><ul>
<li><a class="reference internal" href="#asyncore-example-basic-http-client">asyncore 示例基本 HTTP 客户端</a></li>
<li><a class="reference internal" href="#asyncore-example-basic-echo-server">asyncore 示例基本回显服务器</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="selectors.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code> --- 高级 I/O 复用库</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="asynchat.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asynchat</span></code> --- 异步套接字指令/响应处理程序</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/asyncore.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/asyncore.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="asynchat.html" title="asynchat --- 异步套接字指令/响应处理程序"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="selectors.html" title="selectors --- 高级 I/O 复用库"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">网络和进程间通信</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-asyncore">
<span id="asyncore-asynchronous-socket-handler"></span><h1><a class="reference internal" href="#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncore</span></code></a> --- 异步套接字处理器<a class="headerlink" href="#module-asyncore" title="永久链接至标题"></a></h1>
<p><strong>源码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/asyncore.py">Lib/asyncore.py</a></p>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.6 版后已移除: </span>请使用 <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> 替代。</p>
</div>
<hr class="docutils" />
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>该模块仅为提供向后兼容。我们推荐在新代码中使用 <a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a></p>
</div>
<p>该模块提供用于编写异步套接字服务客户端与服务端的基础构件。</p>
<p>只有两种方法让单个处理器上的程序“同一时间完成不止一件事”。 多线程编程是最简单和最流行的方法,但是还有另一种非常不同的技术,它可以让你拥有多线程的几乎所有优点,而无需实际使用多线程。 它仅仅在你的程序主要受 I/O 限制时有用,那么。 如果你的程序受处理器限制,那么先发制人的预定线程可能就是你真正需要的。 但是,网络服务器很少受处理器限制。</p>
<p>如果你的操作系统在其 I/O 库中支持 <code class="xref c c-func docutils literal notranslate"><span class="pre">select()</span></code> 系统调用(几乎所有操作系统),那么你可以使用它来同时处理多个通信通道;在 I/O 正在“后台”时进行其他工作。 虽然这种策略看起来很奇怪和复杂,特别是起初,它在很多方面比多线程编程更容易理解和控制。 <a class="reference internal" href="#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncore</span></code></a> 模块为您解决了许多难题,使得构建复杂的高性能网络服务器和客户端的任务变得轻而易举。 对于“会话”应用程序和协议,伴侣 <a class="reference internal" href="asynchat.html#module-asynchat" title="asynchat: Support for asynchronous command/response protocols."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asynchat</span></code></a> 模块是非常宝贵的。</p>
<p>这两个模块背后的基本思想是创建一个或多个网络 <em>通道</em> ,类的实例 <a class="reference internal" href="#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncore.dispatcher</span></code></a><a class="reference internal" href="asynchat.html#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal notranslate"><span class="pre">asynchat.async_chat</span></code></a> 。 创建通道会将它们添加到全局映射中,如果你不为它提供自己的 <em>映射</em> ,则由 <a class="reference internal" href="#asyncore.loop" title="asyncore.loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">loop()</span></code></a> 函数使用。</p>
<p>一旦创建了初始通道,调用 <a class="reference internal" href="#asyncore.loop" title="asyncore.loop"><code class="xref py py-func docutils literal notranslate"><span class="pre">loop()</span></code></a> 函数将激活通道服务,该服务将一直持续到最后一个通道(包括在异步服务期间已添加到映射中的任何通道)关闭。</p>
<dl class="function">
<dt id="asyncore.loop">
<code class="sig-prename descclassname">asyncore.</code><code class="sig-name descname">loop</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">timeout</em><span class="optional">[</span>, <em class="sig-param">use_poll</em><span class="optional">[</span>, <em class="sig-param">map</em><span class="optional">[</span>, <em class="sig-param">count</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.loop" title="永久链接至目标"></a></dt>
<dd><p>进入一个轮询循环,其在循环计数超出或所有打开的通道关闭后终止。 所有参数都是可选的。 <em>count</em> 形参默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code> ,导致循环仅在所有通道关闭时终止。 <em>timeout</em> 形参为适当的 <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a><a class="reference internal" href="select.html#select.poll" title="select.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">poll()</span></code></a> 调用设置超时参数,以秒为单位; 默认值为30秒。 <em>use_poll</em> 形参,如果为 True ,则表示 <a class="reference internal" href="select.html#select.poll" title="select.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">poll()</span></code></a> 应优先使用 <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a> (默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code>)。</p>
<p><em>map</em> 形参是一个条目为所监视通道的字典。 当通道关闭时它们会被从映射中删除。 如果省略 <em>map</em>,则会使用一个全局映射。 通道 (<a class="reference internal" href="#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncore.dispatcher</span></code></a>, <a class="reference internal" href="asynchat.html#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal notranslate"><span class="pre">asynchat.async_chat</span></code></a> 及其子类的实例) 可以在映射中任意混合。</p>
</dd></dl>
<dl class="class">
<dt id="asyncore.dispatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncore.</code><code class="sig-name descname">dispatcher</code><a class="headerlink" href="#asyncore.dispatcher" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">dispatcher</span></code></a> 类是对低层级套接字对象的轻量包装器。 要让它更有用处,可以从异步循环调用一些事件处理方法。 在其他方面,它可以被当作是普通的非阻塞型套接字对象。</p>
<p>在特定时间或特定连接状态下触发的低层级事件可通知异步循环发生了特定的高层级事件。 例如,如果我们请求了一个套接字以连接到另一台主机,我们会在套接字首次变得可写时得知连接已建立(在此刻你将知道可以向其写入并预期能够成功)。 包含的高层级事件有:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 35%" />
<col style="width: 65%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>事件</p></th>
<th class="head"><p>描述</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">handle_connect()</span></code></p></td>
<td><p>由首个读取或写入事件引起</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">handle_close()</span></code></p></td>
<td><p>由不带可用数据的读取事件引起</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">handle_accepted()</span></code></p></td>
<td><p>由在监听套接字上的读取事件引起</p></td>
</tr>
</tbody>
</table>
<p>在异步处理过程中,每个已映射通道的 <a class="reference internal" href="#asyncore.dispatcher.readable" title="asyncore.dispatcher.readable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readable()</span></code></a><a class="reference internal" href="#asyncore.dispatcher.writable" title="asyncore.dispatcher.writable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writable()</span></code></a> 方法会被用来确定是否要将通道的套接字添加到已执行 <code class="xref c c-func docutils literal notranslate"><span class="pre">select()</span></code><code class="xref c c-func docutils literal notranslate"><span class="pre">poll()</span></code> 用于读取和写入事件的通道列表中。</p>
<p>因此,通道事件的集合要大于基本套接字事件。 可以在你的子类中被重载的全部方法集合如下:</p>
<dl class="method">
<dt id="asyncore.dispatcher.handle_read">
<code class="sig-name descname">handle_read</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_read" title="永久链接至目标"></a></dt>
<dd><p>当异步循环检测到通道的套接字上的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> 调用将要成功时会被调用。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.handle_write">
<code class="sig-name descname">handle_write</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_write" title="永久链接至目标"></a></dt>
<dd><p>当异步循环检测到一个可写套接字可以被写入时会被调用。 通常此方法将实现必要的缓冲机制以保证运行效率。 例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">handle_write</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">sent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">buffer</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffer</span><span class="p">[</span><span class="n">sent</span><span class="p">:]</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.handle_expt">
<code class="sig-name descname">handle_expt</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_expt" title="永久链接至目标"></a></dt>
<dd><p>当一个套接字连接存在带外OOB数据时会被调用。 这几乎从来不会发生,因为 OOB 虽然受支持但很少被使用。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.handle_connect">
<code class="sig-name descname">handle_connect</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_connect" title="永久链接至目标"></a></dt>
<dd><p>当活动打开方的套接字实际建立连接时会被调用。 可能会发送一条“欢迎”消息,或者向远程端点发起协议协商等。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.handle_close">
<code class="sig-name descname">handle_close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_close" title="永久链接至目标"></a></dt>
<dd><p>当套接字关闭时会被调用。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.handle_error">
<code class="sig-name descname">handle_error</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_error" title="永久链接至目标"></a></dt>
<dd><p>当一个异常被引发并且未获得其他处理时会被调用。 默认版本将打印精简的回溯信息。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.handle_accept">
<code class="sig-name descname">handle_accept</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_accept" title="永久链接至目标"></a></dt>
<dd><p>当可以与发起对本地端点的 <a class="reference internal" href="#asyncore.dispatcher.connect" title="asyncore.dispatcher.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code></a> 调用的新远程端点建立连接时会在侦听通道(被动打开方)上被调用。 在 3.2 版中已被弃用;请改用 <a class="reference internal" href="#asyncore.dispatcher.handle_accepted" title="asyncore.dispatcher.handle_accepted"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_accepted()</span></code></a></p>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.2 版后已移除.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.handle_accepted">
<code class="sig-name descname">handle_accepted</code><span class="sig-paren">(</span><em class="sig-param">sock</em>, <em class="sig-param">addr</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.handle_accepted" title="永久链接至目标"></a></dt>
<dd><p>当与发起对本地端点的 <a class="reference internal" href="#asyncore.dispatcher.connect" title="asyncore.dispatcher.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code></a> 调用的新远程端点已建立连接时会在侦听通道(被动打开方)上被调用。 <em>sock</em> 是可被用于在连接上发送和接收数据的 <em>新建</em> 套接字对象,而 <em>addr</em> 是绑定到连接另一端的套接字的地址。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.readable">
<code class="sig-name descname">readable</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.readable" title="永久链接至目标"></a></dt>
<dd><p>每次在异步循环之外被调用以确定是否应当将一个通道的套接字添加到可能在其上发生读取事件的列表中。 默认方法会简单地返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>,表示在默认情况下,所有通道都希望能读取事件。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.writable">
<code class="sig-name descname">writable</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.writable" title="永久链接至目标"></a></dt>
<dd><p>每次在异步循环之外被调用以确定是否应当将一个通道的套接字添加到可能在其上发生写入事件的列表中。 默认方法会简单地返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>,表示在默认情况下,所有通道都希望能写入事件。</p>
</dd></dl>
<p>此外,每个通道都委托或扩展了许多套接字方法。 它们大部分都与其套接字的对应方法几乎一样。</p>
<dl class="method">
<dt id="asyncore.dispatcher.create_socket">
<code class="sig-name descname">create_socket</code><span class="sig-paren">(</span><em class="sig-param">family=socket.AF_INET</em>, <em class="sig-param">type=socket.SOCK_STREAM</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.create_socket" title="永久链接至目标"></a></dt>
<dd><p>这与普通套接字的创建相同,并会使用同样的创建选项。 请参阅 <a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> 文档了解有关创建套接字的信息。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>family</em><em>type</em> 参数可以被省略。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.connect">
<code class="sig-name descname">connect</code><span class="sig-paren">(</span><em class="sig-param">address</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.connect" title="永久链接至目标"></a></dt>
<dd><p>与普通套接字对象一样,<em>address</em> 是一个元组,它的第一个元素是要连接的主机,第二个元素是端口号。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.send">
<code class="sig-name descname">send</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.send" title="永久链接至目标"></a></dt>
<dd><p><em>data</em> 发送到套接字的远程端点。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.recv">
<code class="sig-name descname">recv</code><span class="sig-paren">(</span><em class="sig-param">buffer_size</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.recv" title="永久链接至目标"></a></dt>
<dd><p>从套接字的远程端点读取至多 <em>buffer_size</em> 个字节。 读到空字节串表明通道已从另一端被关闭。</p>
<p>请注意 <a class="reference internal" href="#asyncore.dispatcher.recv" title="asyncore.dispatcher.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code></a> 可能会引发 <a class="reference internal" href="exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a>,即使 <a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.select()</span></code></a><a class="reference internal" href="select.html#select.poll" title="select.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.poll()</span></code></a> 报告套接字已准备好被读取。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.listen">
<code class="sig-name descname">listen</code><span class="sig-paren">(</span><em class="sig-param">backlog</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.listen" title="永久链接至目标"></a></dt>
<dd><p>侦听与套接字的连接。 <em>backlog</em> 参数指明排入连接队列的最大数量且至少应为 1最大值取决于具体系统通常为 5</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.bind">
<code class="sig-name descname">bind</code><span class="sig-paren">(</span><em class="sig-param">address</em><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.bind" title="永久链接至目标"></a></dt>
<dd><p>将套接字绑定到 <em>address</em>。 套接字必须尚未被绑定。 (<em>address</em> 的格式取决于具体的地址族 --- 请参阅 <a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> 文档了解更多信息。) 要将套接字标记为可重用的 (设置 <code class="xref py py-const docutils literal notranslate"><span class="pre">SO_REUSEADDR</span></code> 选项),请调用 <a class="reference internal" href="#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">dispatcher</span></code></a> 对象的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">set_reuse_addr()</span></code> 方法。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.accept">
<code class="sig-name descname">accept</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.accept" title="永久链接至目标"></a></dt>
<dd><p>接受一个连接。 此套接字必须绑定到一个地址上并且侦听连接。 返回值可以是 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或一个 <code class="docutils literal notranslate"><span class="pre">(conn,</span> <span class="pre">address)</span></code> 对,其中 <em>conn</em> 是一个可用来在此连接上发送和接收数据的 <em>新的</em> 套接字对象,而 <em>address</em> 是绑定到连接另一端套接字的地址。 当返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时意味着连接没有建立,在此情况下服务器应当忽略此事件并继续侦听后续的入站连接。</p>
</dd></dl>
<dl class="method">
<dt id="asyncore.dispatcher.close">
<code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#asyncore.dispatcher.close" title="永久链接至目标"></a></dt>
<dd><p>关闭套接字。 在此套接字对象上的后续操作都将失败。 远程端点将不再接收任何数据(在排入队列的数据被清空之后)。 当套接字被垃圾回收时会自动关闭。</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="asyncore.dispatcher_with_send">
<em class="property">class </em><code class="sig-prename descclassname">asyncore.</code><code class="sig-name descname">dispatcher_with_send</code><a class="headerlink" href="#asyncore.dispatcher_with_send" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">dispatcher</span></code></a> 的一个添加了简单缓冲输出功能的子类,适用于简单客户端。 对于更复杂的用法请使用 <a class="reference internal" href="asynchat.html#asynchat.async_chat" title="asynchat.async_chat"><code class="xref py py-class docutils literal notranslate"><span class="pre">asynchat.async_chat</span></code></a></p>
</dd></dl>
<dl class="class">
<dt id="asyncore.file_dispatcher">
<em class="property">class </em><code class="sig-prename descclassname">asyncore.</code><code class="sig-name descname">file_dispatcher</code><a class="headerlink" href="#asyncore.file_dispatcher" title="永久链接至目标"></a></dt>
<dd><p>file_dispatcher 接受一个文件描述符或 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> 以及一个可选的 map 参数,并对其进行包装以配合 <code class="xref c c-func docutils literal notranslate"><span class="pre">poll()</span></code><code class="xref c c-func docutils literal notranslate"><span class="pre">loop()</span></code> 函数使用。 如果提供一个文件对象或任何具有 <code class="xref c c-func docutils literal notranslate"><span class="pre">fileno()</span></code> 方法的对象,其方法将被调用并传递给 <a class="reference internal" href="#asyncore.file_wrapper" title="asyncore.file_wrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">file_wrapper</span></code></a> 构造器。</p>
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix。</p>
</dd></dl>
<dl class="class">
<dt id="asyncore.file_wrapper">
<em class="property">class </em><code class="sig-prename descclassname">asyncore.</code><code class="sig-name descname">file_wrapper</code><a class="headerlink" href="#asyncore.file_wrapper" title="永久链接至目标"></a></dt>
<dd><p>file_wrapper 接受一个整数形式的文件描述符并调用 <a class="reference internal" href="os.html#os.dup" title="os.dup"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.dup()</span></code></a> 来复制其句柄,以便原始句柄可以独立于 file_wrapper 被关闭。 这个类实现了足够的方法来模拟套接字以供 <a class="reference internal" href="#asyncore.file_dispatcher" title="asyncore.file_dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">file_dispatcher</span></code></a> 类使用。</p>
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix。</p>
</dd></dl>
<section id="asyncore-example-basic-http-client">
<span id="asyncore-example-1"></span><h2>asyncore 示例基本 HTTP 客户端<a class="headerlink" href="#asyncore-example-basic-http-client" title="永久链接至标题"></a></h2>
<p>下面是一个非常基本的 HTTP 客户端,它使用了 <a class="reference internal" href="#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">dispatcher</span></code></a> 类来实现套接字处理:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncore</span>
<span class="k">class</span> <span class="nc">HTTPClient</span><span class="p">(</span><span class="n">asyncore</span><span class="o">.</span><span class="n">dispatcher</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
<span class="n">asyncore</span><span class="o">.</span><span class="n">dispatcher</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">create_socket</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span> <span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span> <span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">buffer</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="s1">&#39;GET </span><span class="si">%s</span><span class="s1"> HTTP/1.0</span><span class="se">\r\n</span><span class="s1">Host: </span><span class="si">%s</span><span class="se">\r\n\r\n</span><span class="s1">&#39;</span> <span class="o">%</span>
<span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">host</span><span class="p">),</span> <span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">handle_connect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">handle_close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">handle_read</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">8192</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">writable</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">buffer</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">handle_write</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">sent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">buffer</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffer</span><span class="p">[</span><span class="n">sent</span><span class="p">:]</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">HTTPClient</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">)</span>
<span class="n">asyncore</span><span class="o">.</span><span class="n">loop</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="asyncore-example-basic-echo-server">
<span id="asyncore-example-2"></span><h2>asyncore 示例基本回显服务器<a class="headerlink" href="#asyncore-example-basic-echo-server" title="永久链接至标题"></a></h2>
<p>下面是一个基本的回显服务器,它使用了 <a class="reference internal" href="#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">dispatcher</span></code></a> 类来接受连接并将入站连接发送给处理程序:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncore</span>
<span class="k">class</span> <span class="nc">EchoHandler</span><span class="p">(</span><span class="n">asyncore</span><span class="o">.</span><span class="n">dispatcher_with_send</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">handle_read</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">8192</span><span class="p">)</span>
<span class="k">if</span> <span class="n">data</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">EchoServer</span><span class="p">(</span><span class="n">asyncore</span><span class="o">.</span><span class="n">dispatcher</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
<span class="n">asyncore</span><span class="o">.</span><span class="n">dispatcher</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">create_socket</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">set_reuse_addr</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bind</span><span class="p">((</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">handle_accepted</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">addr</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Incoming connection from </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">addr</span><span class="p">))</span>
<span class="n">handler</span> <span class="o">=</span> <span class="n">EchoHandler</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
<span class="n">server</span> <span class="o">=</span> <span class="n">EchoServer</span><span class="p">(</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">8080</span><span class="p">)</span>
<span class="n">asyncore</span><span class="o">.</span><span class="n">loop</span><span class="p">()</span>
</pre></div>
</div>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncore</span></code> --- 异步套接字处理器</a><ul>
<li><a class="reference internal" href="#asyncore-example-basic-http-client">asyncore 示例基本 HTTP 客户端</a></li>
<li><a class="reference internal" href="#asyncore-example-basic-echo-server">asyncore 示例基本回显服务器</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="selectors.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code> --- 高级 I/O 复用库</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="asynchat.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">asynchat</span></code> --- 异步套接字指令/响应处理程序</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/asyncore.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="asynchat.html" title="asynchat --- 异步套接字指令/响应处理程序"
>下一页</a> |</li>
<li class="right" >
<a href="selectors.html" title="selectors --- 高级 I/O 复用库"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="ipc.html" >网络和进程间通信</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>