Python Library TCP Socket
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.html 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <!DOCTYPE html>
  2. <html class="writer-html4" lang="en" >
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Welcome to tcp_socket’s documentation! &mdash; tcp_socket documentation</title>
  7. <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <!--[if lt IE 9]>
  10. <script src="_static/js/html5shiv.min.js"></script>
  11. <![endif]-->
  12. <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
  13. <script type="text/javascript" src="_static/jquery.js"></script>
  14. <script type="text/javascript" src="_static/underscore.js"></script>
  15. <script type="text/javascript" src="_static/doctools.js"></script>
  16. <script type="text/javascript" src="_static/language_data.js"></script>
  17. <script type="text/javascript" src="_static/js/theme.js"></script>
  18. <link rel="index" title="Index" href="genindex.html" />
  19. <link rel="search" title="Search" href="search.html" />
  20. </head>
  21. <body class="wy-body-for-nav">
  22. <div class="wy-grid-for-nav">
  23. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  24. <div class="wy-side-scroll">
  25. <div class="wy-side-nav-search" >
  26. <a href="#" class="icon icon-home" alt="Documentation Home"> tcp_socket
  27. </a>
  28. <div role="search">
  29. <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
  30. <input type="text" name="q" placeholder="Search docs" />
  31. <input type="hidden" name="check_keywords" value="yes" />
  32. <input type="hidden" name="area" value="default" />
  33. </form>
  34. </div>
  35. </div>
  36. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  37. <!-- Local TOC -->
  38. <div class="local-toc"><ul>
  39. <li><a class="reference internal" href="#">Welcome to tcp_socket’s documentation!</a><ul>
  40. <li><a class="reference internal" href="#tcp-socket-tcp-socket">tcp_socket (TCP Socket)</a></li>
  41. </ul>
  42. </li>
  43. <li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
  44. </ul>
  45. </div>
  46. </div>
  47. </div>
  48. </nav>
  49. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  50. <nav class="wy-nav-top" aria-label="top navigation">
  51. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  52. <a href="#">tcp_socket</a>
  53. </nav>
  54. <div class="wy-nav-content">
  55. <div class="rst-content">
  56. <div role="navigation" aria-label="breadcrumbs navigation">
  57. <ul class="wy-breadcrumbs">
  58. <li><a href="#" class="icon icon-home"></a> &raquo;</li>
  59. <li>Welcome to tcp_socket’s documentation!</li>
  60. <li class="wy-breadcrumbs-aside">
  61. <a href="_sources/index.rst.txt" rel="nofollow"> View page source</a>
  62. </li>
  63. </ul>
  64. <hr/>
  65. </div>
  66. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  67. <div itemprop="articleBody">
  68. <div class="section" id="module-tcp_socket">
  69. <span id="welcome-to-tcp-socket-s-documentation"></span><h1>Welcome to tcp_socket’s documentation!<a class="headerlink" href="#module-tcp_socket" title="Permalink to this headline">¶</a></h1>
  70. <div class="section" id="tcp-socket-tcp-socket">
  71. <h2>tcp_socket (TCP Socket)<a class="headerlink" href="#tcp-socket-tcp-socket" title="Permalink to this headline">¶</a></h2>
  72. <p><strong>Author:</strong></p>
  73. <ul class="simple">
  74. <li>Dirk Alders &lt;<a class="reference external" href="mailto:sudo-dirk&#37;&#52;&#48;mount-mockery&#46;de">sudo-dirk<span>&#64;</span>mount-mockery<span>&#46;</span>de</a>&gt;</li>
  75. </ul>
  76. <p><strong>Description:</strong></p>
  77. <blockquote>
  78. <div>This Module supports a client/ server tcp socket connection.</div></blockquote>
  79. <p><strong>Submodules:</strong></p>
  80. <ul class="simple">
  81. <li><a class="reference internal" href="#tcp_socket.tcp_client" title="tcp_socket.tcp_client"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_socket.tcp_client</span></code></a></li>
  82. <li><a class="reference internal" href="#tcp_socket.tcp_client_stp" title="tcp_socket.tcp_client_stp"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_socket.tcp_client_stp</span></code></a></li>
  83. <li><a class="reference internal" href="#tcp_socket.tcp_server" title="tcp_socket.tcp_server"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_socket.tcp_server</span></code></a></li>
  84. <li><a class="reference internal" href="#tcp_socket.tcp_server_stp" title="tcp_socket.tcp_server_stp"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_socket.tcp_server_stp</span></code></a></li>
  85. </ul>
  86. <p><strong>Unittest:</strong></p>
  87. <blockquote>
  88. <div>See also the <a class="reference download internal" download="" href="_downloads/e4086ba3197d1869e430d0a75a9a48e9/unittest.pdf"><code class="xref download docutils literal notranslate"><span class="pre">unittest</span></code></a> documentation.</div></blockquote>
  89. <p><strong>Module Documentation:</strong></p>
  90. <dl class="class">
  91. <dt id="tcp_socket.tcp_base">
  92. <em class="property">class </em><code class="descclassname">tcp_socket.</code><code class="descname">tcp_base</code><span class="sig-paren">(</span><em>host</em>, <em>port</em>, <em>channel_name=None</em>, <em>rx_tx_log_lvl=20</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base" title="Permalink to this definition">¶</a></dt>
  93. <dd><p>This is the base class for other classes in this module.</p>
  94. <table class="docutils field-list" frame="void" rules="none">
  95. <col class="field-name" />
  96. <col class="field-body" />
  97. <tbody valign="top">
  98. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  99. <li><strong>host</strong> (<em>str</em>) – The host IP for the TCP socket functionality</li>
  100. <li><strong>port</strong> (<em>int</em>) – The port for the TCP socket functionality</li>
  101. <li><strong>channel_name</strong> (<em>str</em>) – The name for the logging channel</li>
  102. </ul>
  103. </td>
  104. </tr>
  105. </tbody>
  106. </table>
  107. <div class="admonition note">
  108. <p class="first admonition-title">Note</p>
  109. <p class="last">This class is not designed for direct usage.</p>
  110. </div>
  111. <dl class="method">
  112. <dt id="tcp_socket.tcp_base.client_address">
  113. <code class="descname">client_address</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.client_address" title="Permalink to this definition">¶</a></dt>
  114. <dd><p>This method returns the address of the connected client.</p>
  115. <table class="docutils field-list" frame="void" rules="none">
  116. <col class="field-name" />
  117. <col class="field-body" />
  118. <tbody valign="top">
  119. <tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The client address.</td>
  120. </tr>
  121. <tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">str</td>
  122. </tr>
  123. </tbody>
  124. </table>
  125. </dd></dl>
  126. <dl class="method">
  127. <dt id="tcp_socket.tcp_base.close">
  128. <code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.close" title="Permalink to this definition">¶</a></dt>
  129. <dd><p>This method closes the connected communication channel, if exists.</p>
  130. </dd></dl>
  131. <dl class="method">
  132. <dt id="tcp_socket.tcp_base.init_channel_name">
  133. <code class="descname">init_channel_name</code><span class="sig-paren">(</span><em>channel_name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.init_channel_name" title="Permalink to this definition">¶</a></dt>
  134. <dd><p>With this Method, the channel name for logging can be changed.</p>
  135. <table class="docutils field-list" frame="void" rules="none">
  136. <col class="field-name" />
  137. <col class="field-body" />
  138. <tbody valign="top">
  139. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>channel_name</strong> (<em>str</em>) – The name for the logging channel</td>
  140. </tr>
  141. </tbody>
  142. </table>
  143. </dd></dl>
  144. <dl class="method">
  145. <dt id="tcp_socket.tcp_base.is_connected">
  146. <code class="descname">is_connected</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.is_connected" title="Permalink to this definition">¶</a></dt>
  147. <dd><p>With this Method the connection status can be identified.</p>
  148. <table class="docutils field-list" frame="void" rules="none">
  149. <col class="field-name" />
  150. <col class="field-body" />
  151. <tbody valign="top">
  152. <tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">True, if a connection is established, otherwise False.</td>
  153. </tr>
  154. <tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">bool</td>
  155. </tr>
  156. </tbody>
  157. </table>
  158. </dd></dl>
  159. <dl class="method">
  160. <dt id="tcp_socket.tcp_base.receive">
  161. <code class="descname">receive</code><span class="sig-paren">(</span><em>timeout=1</em>, <em>num=None</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.receive" title="Permalink to this definition">¶</a></dt>
  162. <dd><p>This method returns received data.</p>
  163. <table class="docutils field-list" frame="void" rules="none">
  164. <col class="field-name" />
  165. <col class="field-body" />
  166. <tbody valign="top">
  167. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  168. <li><strong>timeout</strong> (<em>float</em>) – The timeout for receiving data (at least after the timeout the method returns data or None).</li>
  169. <li><strong>num</strong> (<em>int</em>) – the number of bytes to receive (use None to get all available data).</li>
  170. </ul>
  171. </td>
  172. </tr>
  173. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The received data.</p>
  174. </td>
  175. </tr>
  176. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">bytes</p>
  177. </td>
  178. </tr>
  179. </tbody>
  180. </table>
  181. </dd></dl>
  182. <dl class="method">
  183. <dt id="tcp_socket.tcp_base.register_callback">
  184. <code class="descname">register_callback</code><span class="sig-paren">(</span><em>callback</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.register_callback" title="Permalink to this definition">¶</a></dt>
  185. <dd><p>This method stores the callback which is executed, if data is available. You need to execute <a class="reference internal" href="#tcp_socket.tcp_base.receive" title="tcp_socket.tcp_base.receive"><code class="xref py py-func docutils literal notranslate"><span class="pre">receive()</span></code></a> of this instance
  186. given as first argument.</p>
  187. <table class="docutils field-list" frame="void" rules="none">
  188. <col class="field-name" />
  189. <col class="field-body" />
  190. <tbody valign="top">
  191. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>callback</strong> – The callback which will be executed, when data is available.</td>
  192. </tr>
  193. </tbody>
  194. </table>
  195. </dd></dl>
  196. <dl class="method">
  197. <dt id="tcp_socket.tcp_base.register_connect_callback">
  198. <code class="descname">register_connect_callback</code><span class="sig-paren">(</span><em>callback</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.register_connect_callback" title="Permalink to this definition">¶</a></dt>
  199. <dd><p>This method stores the callback which is executed, if a connection is established.</p>
  200. <table class="docutils field-list" frame="void" rules="none">
  201. <col class="field-name" />
  202. <col class="field-body" />
  203. <tbody valign="top">
  204. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>callback</strong> – The callback which will be executed, when a connection is established.</td>
  205. </tr>
  206. </tbody>
  207. </table>
  208. </dd></dl>
  209. <dl class="method">
  210. <dt id="tcp_socket.tcp_base.register_disconnect_callback">
  211. <code class="descname">register_disconnect_callback</code><span class="sig-paren">(</span><em>callback</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.register_disconnect_callback" title="Permalink to this definition">¶</a></dt>
  212. <dd><p>This method stores the callback which is executed, after the connection is lost.</p>
  213. <table class="docutils field-list" frame="void" rules="none">
  214. <col class="field-name" />
  215. <col class="field-body" />
  216. <tbody valign="top">
  217. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>callback</strong> – The callback which will be executed, after the connection is lost.</td>
  218. </tr>
  219. </tbody>
  220. </table>
  221. </dd></dl>
  222. <dl class="method">
  223. <dt id="tcp_socket.tcp_base.send">
  224. <code class="descname">send</code><span class="sig-paren">(</span><em>data</em>, <em>timeout=1</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base.send" title="Permalink to this definition">¶</a></dt>
  225. <dd><p>This method sends data via the initiated communication channel.</p>
  226. <table class="docutils field-list" frame="void" rules="none">
  227. <col class="field-name" />
  228. <col class="field-body" />
  229. <tbody valign="top">
  230. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  231. <li><strong>data</strong> (<em>bytes</em>) – The data to be send over the communication channel.</li>
  232. <li><strong>timeout</strong> (<em>float</em>) – The timeout for sending data (e.g. time to establish new connection).</li>
  233. </ul>
  234. </td>
  235. </tr>
  236. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">True if data had been sent, otherwise False.</p>
  237. </td>
  238. </tr>
  239. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">bool</p>
  240. </td>
  241. </tr>
  242. </tbody>
  243. </table>
  244. </dd></dl>
  245. </dd></dl>
  246. <dl class="class">
  247. <dt id="tcp_socket.tcp_base_stp">
  248. <em class="property">class </em><code class="descclassname">tcp_socket.</code><code class="descname">tcp_base_stp</code><span class="sig-paren">(</span><em>host</em>, <em>port</em>, <em>channel_name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base_stp" title="Permalink to this definition">¶</a></dt>
  249. <dd><p>This is the base class for other classes in this module. See also parent <a class="reference internal" href="#tcp_socket.tcp_base" title="tcp_socket.tcp_base"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_base</span></code></a>.</p>
  250. <table class="docutils field-list" frame="void" rules="none">
  251. <col class="field-name" />
  252. <col class="field-body" />
  253. <tbody valign="top">
  254. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  255. <li><strong>host</strong> (<em>str</em>) – The host IP for the TCP socket functionality</li>
  256. <li><strong>port</strong> (<em>int</em>) – The port for the TCP socket functionality</li>
  257. <li><strong>channel_name</strong> (<em>str</em>) – The name for the logging channel</li>
  258. </ul>
  259. </td>
  260. </tr>
  261. </tbody>
  262. </table>
  263. <div class="admonition note">
  264. <p class="first admonition-title">Note</p>
  265. <p class="last">This class is not designed for direct usage.</p>
  266. </div>
  267. <dl class="method">
  268. <dt id="tcp_socket.tcp_base_stp.receive">
  269. <code class="descname">receive</code><span class="sig-paren">(</span><em>timeout=1</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base_stp.receive" title="Permalink to this definition">¶</a></dt>
  270. <dd><p>This method returns one received messages via the initiated communication channel.</p>
  271. <table class="docutils field-list" frame="void" rules="none">
  272. <col class="field-name" />
  273. <col class="field-body" />
  274. <tbody valign="top">
  275. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>timeout</strong> (<em>float</em>) – The timeout for receiving data (at least after the timeout the method returns data or None).</td>
  276. </tr>
  277. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">The received data.</td>
  278. </tr>
  279. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">bytes</td>
  280. </tr>
  281. </tbody>
  282. </table>
  283. </dd></dl>
  284. <dl class="method">
  285. <dt id="tcp_socket.tcp_base_stp.send">
  286. <code class="descname">send</code><span class="sig-paren">(</span><em>data</em>, <em>timeout=1</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_base_stp.send" title="Permalink to this definition">¶</a></dt>
  287. <dd><p>This method sends one stp message via the initiated communication channel.</p>
  288. <table class="docutils field-list" frame="void" rules="none">
  289. <col class="field-name" />
  290. <col class="field-body" />
  291. <tbody valign="top">
  292. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  293. <li><strong>data</strong> (<em>bytes</em>) – The message to be send over the communication channel.</li>
  294. <li><strong>timeout</strong> (<em>float</em>) – The timeout for sending data (e.g. time to establish new connection).</li>
  295. </ul>
  296. </td>
  297. </tr>
  298. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">True if data had been sent, otherwise False.</p>
  299. </td>
  300. </tr>
  301. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">bool</p>
  302. </td>
  303. </tr>
  304. </tbody>
  305. </table>
  306. </dd></dl>
  307. </dd></dl>
  308. <dl class="class">
  309. <dt id="tcp_socket.tcp_client">
  310. <em class="property">class </em><code class="descclassname">tcp_socket.</code><code class="descname">tcp_client</code><span class="sig-paren">(</span><em>host</em>, <em>port</em>, <em>channel_name=None</em>, <em>rx_tx_log_lvl=20</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_client" title="Permalink to this definition">¶</a></dt>
  311. <dd><p>This class creates a tcp-client for transfering a serial stream of bytes (characters). See also parent <a class="reference internal" href="#tcp_socket.tcp_base" title="tcp_socket.tcp_base"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_base</span></code></a>.</p>
  312. <table class="docutils field-list" frame="void" rules="none">
  313. <col class="field-name" />
  314. <col class="field-body" />
  315. <tbody valign="top">
  316. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  317. <li><strong>host</strong> (<em>str</em>) – The host IP for the TCP socket functionality</li>
  318. <li><strong>port</strong> (<em>int</em>) – The port for the TCP socket functionality</li>
  319. <li><strong>channel_name</strong> (<em>str</em>) – The name for the logging channel</li>
  320. </ul>
  321. </td>
  322. </tr>
  323. </tbody>
  324. </table>
  325. <div class="admonition note">
  326. <p class="first admonition-title">Note</p>
  327. <p class="last">You need a running <a class="reference internal" href="#tcp_socket.tcp_server" title="tcp_socket.tcp_server"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_server</span></code></a> listening at the given IP and Port to be able to communicate.</p>
  328. </div>
  329. <p><strong>Example:</strong></p>
  330. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
  331. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  332. <span class="kn">import</span> <span class="nn">report</span>
  333. <span class="kn">import</span> <span class="nn">tcp_socket</span>
  334. <span class="kn">import</span> <span class="nn">time</span>
  335. <span class="k">def</span> <span class="nf">mirror_callback</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
  336. <span class="k">return</span> <span class="n">data</span>
  337. <span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;root&#39;</span><span class="p">,</span> <span class="s1">&#39;DEBUG&#39;</span><span class="p">),</span> <span class="p">])</span>
  338. <span class="n">c</span> <span class="o">=</span> <span class="n">tcp_socket</span><span class="o">.</span><span class="n">tcp_client</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">17000</span><span class="p">)</span>
  339. <span class="n">c</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>
  340. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;The Client received: </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">c</span><span class="o">.</span><span class="n">receive</span><span class="p">()))</span>
  341. </pre></div>
  342. </div>
  343. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">183</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  344. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">233</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">Connection</span> <span class="n">established</span><span class="o">...</span> <span class="p">(</span><span class="n">to</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17000</span><span class="p">)</span>
  345. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">234</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  346. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">283</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">TX</span> <span class="o">-&gt;</span> <span class="s2">&quot;(3): 61 62 63&quot;</span>
  347. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">284</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">RX</span> <span class="o">&lt;-</span> <span class="s2">&quot;(3): 61 62 63&quot;</span>
  348. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">334</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  349. <span class="n">The</span> <span class="n">Client</span> <span class="n">received</span><span class="p">:</span> <span class="sa">b</span><span class="s1">&#39;abc&#39;</span>
  350. </pre></div>
  351. </div>
  352. </dd></dl>
  353. <dl class="class">
  354. <dt id="tcp_socket.tcp_client_stp">
  355. <em class="property">class </em><code class="descclassname">tcp_socket.</code><code class="descname">tcp_client_stp</code><span class="sig-paren">(</span><em>host</em>, <em>port</em>, <em>channel_name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_client_stp" title="Permalink to this definition">¶</a></dt>
  356. <dd><p>This class creates a tcp-client for transfering a message. The bytes will be packed on send and unpacked on receive. See also parents <a class="reference internal" href="#tcp_socket.tcp_client" title="tcp_socket.tcp_client"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_client</span></code></a> and <a class="reference internal" href="#tcp_socket.tcp_base_stp" title="tcp_socket.tcp_base_stp"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_base_stp</span></code></a>.
  357. See <code class="xref py py-mod docutils literal notranslate"><span class="pre">stringtools.stp</span></code> for more information on packing and unpacking.</p>
  358. <table class="docutils field-list" frame="void" rules="none">
  359. <col class="field-name" />
  360. <col class="field-body" />
  361. <tbody valign="top">
  362. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  363. <li><strong>host</strong> (<em>str</em>) – The host IP for the TCP socket functionality</li>
  364. <li><strong>port</strong> (<em>int</em>) – The port for the TCP socket functionality</li>
  365. <li><strong>channel_name</strong> (<em>str</em>) – The name for the logging channel</li>
  366. </ul>
  367. </td>
  368. </tr>
  369. </tbody>
  370. </table>
  371. <div class="admonition note">
  372. <p class="first admonition-title">Note</p>
  373. <p class="last">You need a running <a class="reference internal" href="#tcp_socket.tcp_server_stp" title="tcp_socket.tcp_server_stp"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_server_stp</span></code></a> listening at the given IP and Port to be able to communicate.</p>
  374. </div>
  375. <p><strong>Example:</strong></p>
  376. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
  377. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  378. <span class="kn">import</span> <span class="nn">report</span>
  379. <span class="kn">import</span> <span class="nn">tcp_socket</span>
  380. <span class="kn">import</span> <span class="nn">time</span>
  381. <span class="k">def</span> <span class="nf">mirror_callback</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
  382. <span class="k">return</span> <span class="n">data</span>
  383. <span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;root&#39;</span><span class="p">,</span> <span class="s1">&#39;DEBUG&#39;</span><span class="p">),</span> <span class="p">])</span>
  384. <span class="n">c</span> <span class="o">=</span> <span class="n">tcp_socket</span><span class="o">.</span><span class="n">tcp_client_stp</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">17017</span><span class="p">)</span>
  385. <span class="n">c</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>
  386. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;The Client received: </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">c</span><span class="o">.</span><span class="n">receive</span><span class="p">()))</span>
  387. </pre></div>
  388. </div>
  389. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">784</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  390. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">839</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">Connection</span> <span class="n">established</span><span class="o">...</span> <span class="p">(</span><span class="n">to</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17017</span><span class="p">)</span>
  391. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">839</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">client</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  392. </pre></div>
  393. </div>
  394. </dd></dl>
  395. <dl class="class">
  396. <dt id="tcp_socket.tcp_server">
  397. <em class="property">class </em><code class="descclassname">tcp_socket.</code><code class="descname">tcp_server</code><span class="sig-paren">(</span><em>host</em>, <em>port</em>, <em>channel_name=None</em>, <em>rx_tx_log_lvl=20</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_server" title="Permalink to this definition">¶</a></dt>
  398. <dd><p>This class creates a tcp-server for transfering a serial stream of bytes (characters). See also parent <a class="reference internal" href="#tcp_socket.tcp_base" title="tcp_socket.tcp_base"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_base</span></code></a>.</p>
  399. <table class="docutils field-list" frame="void" rules="none">
  400. <col class="field-name" />
  401. <col class="field-body" />
  402. <tbody valign="top">
  403. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  404. <li><strong>host</strong> (<em>str</em>) – The host IP for the TCP socket functionality</li>
  405. <li><strong>port</strong> (<em>int</em>) – The port for the TCP socket functionality</li>
  406. <li><strong>channel_name</strong> (<em>str</em>) – The name for the logging channel</li>
  407. </ul>
  408. </td>
  409. </tr>
  410. </tbody>
  411. </table>
  412. <div class="admonition note">
  413. <p class="first admonition-title">Note</p>
  414. <p class="last">You need a <a class="reference internal" href="#tcp_socket.tcp_client" title="tcp_socket.tcp_client"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_client</span></code></a> to communicate with the server.</p>
  415. </div>
  416. <p><strong>Example:</strong></p>
  417. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
  418. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  419. <span class="kn">import</span> <span class="nn">report</span>
  420. <span class="kn">import</span> <span class="nn">tcp_socket</span>
  421. <span class="kn">import</span> <span class="nn">time</span>
  422. <span class="k">def</span> <span class="nf">mirror_callback</span><span class="p">(</span><span class="n">tcp</span><span class="p">):</span>
  423. <span class="n">tcp</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">tcp</span><span class="o">.</span><span class="n">receive</span><span class="p">())</span>
  424. <span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;root&#39;</span><span class="p">,</span> <span class="s1">&#39;DEBUG&#39;</span><span class="p">),</span> <span class="p">])</span>
  425. <span class="n">s</span> <span class="o">=</span> <span class="n">tcp_socket</span><span class="o">.</span><span class="n">tcp_server</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">17000</span><span class="p">)</span>
  426. <span class="n">s</span><span class="o">.</span><span class="n">register_callback</span><span class="p">(</span><span class="n">mirror_callback</span><span class="p">)</span>
  427. <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
  428. <span class="k">while</span> <span class="ow">not</span> <span class="n">s</span><span class="o">.</span><span class="n">is_connected</span><span class="p">()</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="p">:</span>
  429. <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
  430. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># wait for a connection</span>
  431. <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
  432. <span class="k">while</span> <span class="n">s</span><span class="o">.</span><span class="n">is_connected</span><span class="p">()</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="p">:</span>
  433. <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
  434. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># wait for disconnect</span>
  435. </pre></div>
  436. </div>
  437. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">392</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  438. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">392</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Server</span> <span class="n">listening</span> <span class="n">to</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17000</span>
  439. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">544</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Connection</span> <span class="n">established</span><span class="o">...</span> <span class="p">(</span><span class="kn">from</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17000</span><span class="p">)</span>
  440. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">544</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  441. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">594</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">RX</span> <span class="o">&lt;-</span> <span class="s2">&quot;(3): 61 62 63&quot;</span>
  442. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">595</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  443. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">595</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">TX</span> <span class="o">-&gt;</span> <span class="s2">&quot;(3): 61 62 63&quot;</span>
  444. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">747</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Connection</span> <span class="n">lost</span><span class="o">...</span>
  445. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">57</span><span class="p">,</span><span class="mi">748</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Server</span> <span class="n">listening</span> <span class="n">to</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17000</span>
  446. </pre></div>
  447. </div>
  448. </dd></dl>
  449. <dl class="class">
  450. <dt id="tcp_socket.tcp_server_stp">
  451. <em class="property">class </em><code class="descclassname">tcp_socket.</code><code class="descname">tcp_server_stp</code><span class="sig-paren">(</span><em>host</em>, <em>port</em>, <em>channel_name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#tcp_socket.tcp_server_stp" title="Permalink to this definition">¶</a></dt>
  452. <dd><p>This class creates a tcp-server for transfering a message. The bytes will be packed on send and unpacked on receive. See also parents <a class="reference internal" href="#tcp_socket.tcp_server" title="tcp_socket.tcp_server"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_server</span></code></a> and <a class="reference internal" href="#tcp_socket.tcp_base_stp" title="tcp_socket.tcp_base_stp"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_base_stp</span></code></a>.
  453. See <code class="xref py py-mod docutils literal notranslate"><span class="pre">stringtools.stp</span></code> for more information on packing and unpacking.</p>
  454. <table class="docutils field-list" frame="void" rules="none">
  455. <col class="field-name" />
  456. <col class="field-body" />
  457. <tbody valign="top">
  458. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
  459. <li><strong>host</strong> (<em>str</em>) – The host IP for the TCP socket functionality</li>
  460. <li><strong>port</strong> (<em>int</em>) – The port for the TCP socket functionality</li>
  461. <li><strong>channel_name</strong> (<em>str</em>) – The name for the logging channel</li>
  462. </ul>
  463. </td>
  464. </tr>
  465. </tbody>
  466. </table>
  467. <div class="admonition note">
  468. <p class="first admonition-title">Note</p>
  469. <p class="last">You need a <a class="reference internal" href="#tcp_socket.tcp_client_stp" title="tcp_socket.tcp_client_stp"><code class="xref py py-class docutils literal notranslate"><span class="pre">tcp_client_stp</span></code></a> to communicate with the server.</p>
  470. </div>
  471. <p><strong>Example:</strong></p>
  472. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
  473. <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;../..&#39;</span><span class="p">)</span>
  474. <span class="kn">import</span> <span class="nn">report</span>
  475. <span class="kn">import</span> <span class="nn">tcp_socket</span>
  476. <span class="kn">import</span> <span class="nn">time</span>
  477. <span class="k">def</span> <span class="nf">mirror_callback</span><span class="p">(</span><span class="n">tcp</span><span class="p">):</span>
  478. <span class="n">tcp</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">tcp</span><span class="o">.</span><span class="n">receive</span><span class="p">())</span>
  479. <span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;root&#39;</span><span class="p">,</span> <span class="s1">&#39;DEBUG&#39;</span><span class="p">),</span> <span class="p">])</span>
  480. <span class="n">s</span> <span class="o">=</span> <span class="n">tcp_socket</span><span class="o">.</span><span class="n">tcp_server_stp</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">17017</span><span class="p">)</span>
  481. <span class="n">s</span><span class="o">.</span><span class="n">register_callback</span><span class="p">(</span><span class="n">mirror_callback</span><span class="p">)</span>
  482. <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
  483. <span class="k">while</span> <span class="ow">not</span> <span class="n">s</span><span class="o">.</span><span class="n">is_connected</span><span class="p">()</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="p">:</span>
  484. <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
  485. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># wait for a connection</span>
  486. <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
  487. <span class="k">while</span> <span class="n">s</span><span class="o">.</span><span class="n">is_connected</span><span class="p">()</span> <span class="ow">and</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="p">:</span>
  488. <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
  489. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># wait for disconnect</span>
  490. </pre></div>
  491. </div>
  492. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">425</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  493. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">425</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Server</span> <span class="n">listening</span> <span class="n">to</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17017</span>
  494. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">526</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Connection</span> <span class="n">established</span><span class="o">...</span> <span class="p">(</span><span class="kn">from</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17017</span><span class="p">)</span>
  495. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">526</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Cleaning</span> <span class="n">up</span> <span class="n">receive</span><span class="o">-</span><span class="n">buffer</span>
  496. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">627</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">RX</span> <span class="o">&lt;-</span> <span class="s2">&quot;(7): 3a 3c 61 62 63 3a 3e&quot;</span>
  497. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">627</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">stringtools</span><span class="o">.</span><span class="n">stp</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">STP</span><span class="p">:</span> <span class="n">data</span> <span class="n">sync</span> <span class="p">(</span><span class="mi">3</span><span class="n">a</span><span class="p">)</span> <span class="n">received</span> <span class="o">=&gt;</span> <span class="n">changing</span> <span class="n">state</span> <span class="n">STP_STATE_IDLE</span> <span class="o">-&gt;</span> <span class="n">STP_STATE_ESCAPE_1</span>
  498. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">628</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">stringtools</span><span class="o">.</span><span class="n">stp</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">STP</span><span class="p">:</span> <span class="n">start</span> <span class="n">pattern</span> <span class="p">(</span><span class="mi">3</span><span class="n">a</span> <span class="mi">3</span><span class="n">c</span><span class="p">)</span> <span class="n">received</span> <span class="o">=&gt;</span> <span class="n">changing</span> <span class="n">state</span> <span class="n">STP_STATE_ESCAPE_1</span> <span class="o">-&gt;</span> <span class="n">STP_STATE_STORE_DATA</span>
  499. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">628</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">stringtools</span><span class="o">.</span><span class="n">stp</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">STP</span><span class="p">:</span> <span class="n">data</span> <span class="n">sync</span> <span class="p">(</span><span class="mi">3</span><span class="n">a</span><span class="p">)</span> <span class="n">received</span> <span class="o">=&gt;</span> <span class="n">changing</span> <span class="n">state</span> <span class="n">STP_STATE_STORE_DATA</span> <span class="o">-&gt;</span> <span class="n">STP_STATE_ESCAPE_2</span>
  500. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">628</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">stringtools</span><span class="o">.</span><span class="n">stp</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">STP</span><span class="p">:</span> <span class="n">end</span> <span class="n">pattern</span> <span class="p">(</span><span class="mi">3</span><span class="n">a</span> <span class="mi">3</span><span class="n">e</span><span class="p">)</span> <span class="n">received</span> <span class="o">=&gt;</span> <span class="n">storing</span> <span class="n">message</span> <span class="ow">and</span> <span class="n">changing</span> <span class="n">state</span> <span class="n">STP_STATE_ESCAPE_2</span> <span class="o">-&gt;</span> <span class="n">STP_STATE_IDLE</span>
  501. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">628</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">stringtools</span><span class="o">.</span><span class="n">stp</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">STP</span><span class="p">:</span> <span class="n">message</span> <span class="n">identified</span> <span class="o">-</span> <span class="p">(</span><span class="mi">3</span><span class="p">):</span> <span class="mi">61</span> <span class="mi">62</span> <span class="mi">63</span>
  502. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">628</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">RX</span> <span class="o">&lt;-</span> <span class="s2">&quot;(3): 61 62 63&quot;</span>
  503. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">628</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">TX</span> <span class="o">-&gt;</span> <span class="s2">&quot;(7): 3a 3c 61 62 63 3a 3e&quot;</span>
  504. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">629</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">TX</span> <span class="o">-&gt;</span> <span class="s2">&quot;(3): 61 62 63&quot;</span>
  505. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">780</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Connection</span> <span class="n">lost</span><span class="o">...</span>
  506. <span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">11</span> <span class="mi">07</span><span class="p">:</span><span class="mi">17</span><span class="p">:</span><span class="mi">54</span><span class="p">,</span><span class="mi">780</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">tcp_socket</span><span class="o">.</span><span class="n">all_others</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">comm</span><span class="o">-</span><span class="n">server</span><span class="p">:</span> <span class="n">Server</span> <span class="n">listening</span> <span class="n">to</span> <span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">17017</span>
  507. </pre></div>
  508. </div>
  509. </dd></dl>
  510. </div>
  511. <div class="toctree-wrapper compound">
  512. </div>
  513. </div>
  514. <div class="section" id="indices-and-tables">
  515. <h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h1>
  516. <ul class="simple">
  517. <li><a class="reference internal" href="genindex.html"><span class="std std-ref">Index</span></a></li>
  518. <li><a class="reference internal" href="py-modindex.html"><span class="std std-ref">Module Index</span></a></li>
  519. <li><a class="reference internal" href="search.html"><span class="std std-ref">Search Page</span></a></li>
  520. </ul>
  521. </div>
  522. </div>
  523. </div>
  524. <footer>
  525. <hr/>
  526. <div role="contentinfo">
  527. <p>
  528. &copy; Copyright 2021, Dirk Alders
  529. </p>
  530. </div>
  531. Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
  532. <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
  533. provided by <a href="https://readthedocs.org">Read the Docs</a>.
  534. </footer>
  535. </div>
  536. </div>
  537. </section>
  538. </div>
  539. <script type="text/javascript">
  540. jQuery(function () {
  541. SphinxRtdTheme.Navigation.enable(true);
  542. });
  543. </script>
  544. </body>
  545. </html>