Echo Server Using Tornado

本文详细介绍了如何使用Tornado框架实现一个基于TCP的回显服务器,通过动手实践深入理解Tornado的基本用法和TCP通信原理。

這幾天在看Tornado的source code,有一些體會,自己動手寫了一個基於tornado tcpserver的echoserver

#!/usr/bin/env python

from tornado.netutil import TCPServer
from tornado.ioloop  import IOLoop

class ECHOConnection(object):
    def __init__(self, stream):
        self._stream = stream
        self.read()
    
    def read(self):
        self._stream.read_until('\r\n', self.echo_callback)
    
    def echo_callback(self, data):
        print "data:", data
        self._stream.write(data)
        self.read()

class ECHOServer(TCPServer):
    def handle_stream(self, stream, address):
        print "echo connecting..."
        ECHOConnection(stream)

if __name__ == '__main__':
    print "begin..."
    server = ECHOServer()
    server.listen(8000)
    IOLoop.instance().start()    


“ [{ "resource": "/home/wrd/WYH/Brain/projects/scripts/brain_analysis.py", "owner": "Pylance5", "code": { "value": "reportMissingImports", "target": { "$mid": 1, "path": "/microsoft/pylance-release/blob/main/docs/diagnostics/reportMissingImports.md", "scheme": "https", "authority": "github.com" } }, "severity": 4, "message": "无法解析导入“braian”", "source": "Pylance", "startLineNumber": 5, "startColumn": 8, "endLineNumber": 5, "endColumn": 14, "origin": "extHost1" },{ "resource": "/home/wrd/WYH/Brain/projects/scripts/brain_analysis.py", "owner": "Pylance5", "code": { "value": "reportMissingImports", "target": { "$mid": 1, "path": "/microsoft/pylance-release/blob/main/docs/diagnostics/reportMissingImports.md", "scheme": "https", "authority": "github.com" } }, "severity": 4, "message": "无法解析导入“braian.config”", "source": "Pylance", "startLineNumber": 6, "startColumn": 8, "endLineNumber": 6, "endColumn": 21, "origin": "extHost1" },{ "resource": "/home/wrd/WYH/Brain/projects/scripts/brain_analysis.py", "owner": "Pylance5", "code": { "value": "reportMissingImports", "target": { "$mid": 1, "path": "/microsoft/pylance-release/blob/main/docs/diagnostics/reportMissingImports.md", "scheme": "https", "authority": "github.com" } }, "severity": 4, "message": "无法解析导入“braian.plot”", "source": "Pylance", "startLineNumber": 7, "startColumn": 8, "endLineNumber": 7, "endColumn": 19, "origin": "extHost1" },{ "resource": "/home/wrd/WYH/Brain/projects/scripts/brain_analysis.py", "owner": "Pylance5", "code": { "value": "reportMissingImports", "target": { "$mid": 1, "path": "/microsoft/pylance-release/blob/main/docs/diagnostics/reportMissingImports.md", "scheme": "https", "authority": "github.com" } }, "severity": 4, "message": "无法解析导入“braian.stats”", "source": "Pylance", "startLineNumber": 8, "startColumn": 8, "endLineNumber": 8, "endColumn": 20, "origin": "extHost1" }] ”的同时,我的: " wrd@wrd-ThinkPad-X240:~$ pip list Package Version ----------------------------- -------------------- actionlib 1.14.3 angles 1.9.14 anyio 4.5.2 apturl 0.5.2 argon2-cffi 25.1.0 argon2-cffi-bindings 21.2.0 arrow 1.4.0 asttokens 3.0.1 async-lru 2.0.4 attrs 25.3.0 autobahn 17.10.1 Automat 0.8.0 babel 2.17.0 backcall 0.2.0 backports.zoneinfo 0.2.1 bcrypt 3.1.7 beautifulsoup4 4.14.3 bleach 6.1.0 blinker 1.4 bondpy 1.8.7 breezy 3.0.2 Brian2 2.5.1 Brlapi 0.7.0 camera-calibration 1.17.0 camera-calibration-parsers 1.12.1 catkin 0.8.12 catkin-pkg 1.0.0 catkin-pkg-modules 1.0.0 cbor 1.0.0 certifi 2019.11.28 cffi 1.17.1 chardet 3.0.4 charset-normalizer 3.4.4 Click 7.0 colorama 0.4.3 comm 0.2.3 command-not-found 0.3 configobj 5.0.6 constantly 15.1.0 controller-manager 0.20.0 controller-manager-msgs 0.20.0 cryptography 2.8 cupshelpers 1.0 cv-bridge 1.16.2 cycler 0.10.0 Cython 0.29.14 czifile 2019.7.2.1 dbus-python 1.2.16 debugpy 1.8.17 decorator 5.2.1 defer 1.0.6 defusedxml 0.6.0 Deprecated 1.2.7 diagnostic-analysis 1.12.1 diagnostic-common-diagnostics 1.12.1 diagnostic-updater 1.12.1 distro 1.4.0 distro-info 0.23+ubuntu1.1 docutils 0.16 dulwich 0.19.15 duplicity 0.8.12.0 dynamic-reconfigure 1.7.6 empy 3.3.2 entrypoints 0.3 exceptiongroup 1.3.1 executing 2.2.1 fasteners 0.14.1 fastimport 0.9.8 fastjsonschema 2.21.2 fqdn 1.5.1 future 0.18.2 gazebo_plugins 2.9.3 gazebo_ros 2.9.3 gencpp 0.7.2 geneus 3.0.0 genlisp 0.4.18 genmsg 0.6.1 gennodejs 2.0.2 genpy 0.6.18 gevent 24.2.1 gpg 1.13.1 greenlet 3.1.1 h11 0.16.0 httpcore 1.0.9 httplib2 0.14.0 httpx 0.28.1 hyperlink 19.0.0 idna 2.8 image-geometry 1.16.2 imagecodecs 2023.3.16 imageio 2.35.1 importlib_metadata 8.5.0 importlib_resources 6.4.5 incremental 16.10.1 interactive-markers 1.12.2 ipykernel 6.29.5 ipython 8.12.3 ipywidgets 8.1.8 isoduration 20.11.0 jedi 0.19.2 Jinja2 3.1.6 joint-state-publisher 1.15.2 joint-state-publisher-gui 1.15.2 json5 0.12.1 jsonpointer 3.0.0 jsonschema 4.23.0 jsonschema-specifications 2023.12.1 jupyter 1.1.1 jupyter_client 8.6.3 jupyter-console 6.6.3 jupyter_core 5.8.1 jupyter-events 0.10.0 jupyter-lsp 2.3.0 jupyter_server 2.14.2 jupyter_server_terminals 0.5.3 jupyterlab 4.3.8 jupyterlab_pygments 0.3.0 jupyterlab_server 2.28.0 jupyterlab_widgets 3.0.16 keyring 18.0.1 kiwisolver 1.0.1 language-selector 0.1 laser-geometry 1.6.8 launchpadlib 1.10.13 lazr.restfulclient 0.14.2 lazr.uri 1.0.3 lazy_loader 0.4 lockfile 0.12.2 louis 3.12.0 lz4 3.0.2+dfsg macaroonbakery 1.3.1 Mako 1.1.0 MarkupSafe 2.1.5 matplotlib 3.1.2 matplotlib-inline 0.1.7 message-filters 1.17.4 mistune 3.1.4 monotonic 1.5 mpi4py 3.0.3 mpmath 1.3.0 narwhals 1.42.1 nbclient 0.10.1 nbconvert 7.16.6 nbformat 5.10.4 nest-asyncio 1.6.0 netifaces 0.10.4 networkx 3.1 nose 1.3.7 notebook 7.3.3 notebook_shim 0.2.4 numpy 1.24.4 oauthlib 3.1.0 olefile 0.46 opencv-python 4.12.0.88 overrides 7.7.0 packaging 25.0 paho-mqtt 2.1.0 pandas 2.0.3 pandocfilters 1.5.1 paramiko 2.6.0 parso 0.8.5 pexpect 4.6.0 pickleshare 0.7.5 pillow 10.4.0 pip 25.0.1 pkgutil_resolve_name 1.3.10 platformdirs 4.3.6 plotly 6.5.0 prometheus_client 0.21.1 prompt_toolkit 3.0.52 protobuf 3.6.1 psutil 5.5.1 ptyprocess 0.7.0 pure_eval 0.2.3 py-ubjson 0.14.0 pyasn1 0.4.2 pyasn1-modules 0.2.1 pycairo 1.16.2 pycparser 2.23 pycryptodomex 3.6.1 pycups 1.9.73 pydot 1.4.1 PyGithub 1.43.7 Pygments 2.19.2 PyGObject 3.36.0 PyHamcrest 1.9.0 PyJWT 1.7.1 pymacaroons 0.13.0 PyNaCl 1.3.0 PyOpenGL 3.1.0 pyOpenSSL 19.0.0 pyparsing 2.4.6 pypng 0.0.20 PyQRCode 1.2.1 PyQt5 5.14.1 pyRFC3339 1.1 python-apt 2.0.1+ubuntu0.20.4.1 python-dateutil 2.9.0.post0 python-debian 0.1.36+ubuntu1.1 python-gitlab 2.0.1 python-gnupg 0.4.5 python-json-logger 4.0.0 python-qt-binding 0.4.6 python-snappy 0.5.3 PyTrie 0.2 pytz 2025.2 PyWavelets 1.4.1 pyxdg 0.26 PyYAML 5.3.1 pyzmq 27.1.0 qt-dotgraph 0.4.5 qt-gui 0.4.5 qt-gui-cpp 0.4.5 qt-gui-py-common 0.4.5 referencing 0.35.1 reportlab 3.5.34 requests 2.32.4 requests-unixsocket 0.2.0 resource-retriever 1.12.10 rfc3339-validator 0.1.4 rfc3986-validator 0.1.1 roman 2.0.0 rosbag 1.17.4 rosboost-cfg 1.15.10 rosclean 1.15.10 roscreate 1.15.10 rosdep-modules 0.25.1 rosdistro 1.0.1 rosdistro-modules 1.0.1 rosgraph 1.17.4 rosinstall 0.7.8 rosinstall-generator 0.1.23 roslaunch 1.17.4 roslib 1.15.10 roslint 0.12.0 roslz4 1.17.4 rosmake 1.15.10 rosmaster 1.17.4 rosmsg 1.17.4 rosnode 1.17.4 rosparam 1.17.4 rospkg 1.6.0 rospkg-modules 1.6.0 rospy 1.17.4 rosservice 1.17.4 rostest 1.17.4 rostopic 1.17.4 rosunit 1.15.10 roswtf 1.17.4 rpds-py 0.20.1 rpm 0.4.0 rqt-action 0.4.11 rqt-bag 0.5.3 rqt-bag-plugins 0.5.3 rqt-console 0.4.14 rqt-dep 0.4.14 rqt-graph 0.4.16 rqt-gui 0.5.5 rqt-gui-py 0.5.5 rqt-image-view 0.4.19 rqt-launch 0.4.10 rqt-logger-level 0.4.13 rqt-moveit 0.5.13 rqt-msg 0.4.12 rqt-nav-view 0.5.8 rqt-plot 0.4.16 rqt-pose-view 0.5.13 rqt-publisher 0.4.12 rqt-py-common 0.5.5 rqt-py-console 0.4.12 rqt-reconfigure 0.5.7 rqt-robot-dashboard 0.5.8 rqt-robot-monitor 0.5.15 rqt-robot-steering 0.5.14 rqt-runtime-monitor 0.5.10 rqt-rviz 0.7.2 rqt-service-caller 0.4.12 rqt-shell 0.4.13 rqt-srv 0.4.11 rqt-tf-tree 0.6.5 rqt-top 0.4.11 rqt-topic 0.4.15 rqt-web 0.4.11 rviz 1.14.26 scikit-image 0.21.0 scipy 1.10.1 SecretStorage 2.3.1 Send2Trash 1.8.3 sensor-msgs 1.13.2 service-identity 18.1.0 setuptools 45.2.0 simplejson 3.16.0 sip 4.19.21 six 1.14.0 smach 2.5.3 smach-ros 2.5.3 smclib 1.8.7 sniffio 1.3.1 soupsieve 2.7 stack-data 0.6.3 sympy 1.13.3 systemd-python 234 terminado 0.18.1 tf 1.13.4 tf-conversions 1.13.4 tf2-geometry-msgs 0.7.10 tf2-kdl 0.7.10 tf2-py 0.7.10 tf2-ros 0.7.10 tifffile 2023.7.10 tinycss2 1.2.1 tomli 2.3.0 topic-tools 1.17.4 tornado 6.4.2 traitlets 5.14.3 Twisted 18.9.0 txaio 2.10.0 typing_extensions 4.13.2 tzdata 2025.2 u-msgpack-python 2.1 ubuntu-drivers-common 0.0.0 ubuntu-pro-client 8001 ufw 0.36 unattended-upgrades 0.1 uri-template 1.3.0 urllib3 1.25.8 usb-creator 0.3.7 vcstools 0.1.42 wadllib 1.3.3 wcwidth 0.2.14 webcolors 24.8.0 webencodings 0.5.1 websocket 0.2.1 websocket-client 1.8.0 wheel 0.34.2 widgetsnbextension 4.0.15 wrapt 1.11.2 wsaccel 0.6.2 wstool 0.1.18 xacro 1.14.20 xkit 0.0.0 zipp 3.20.2 zope.event 5.0 zope.interface 4.7.1 wrd@wrd-ThinkPad-X240:~$ python3 --version Python 3.8.10 wrd@wrd-ThinkPad-X240:~$ pip install braian Defaulting to user installation because normal site-packages is not writeable ERROR: Could not find a version that satisfies the requirement braian (from versions: none) ERROR: No matching distribution found for braian wrd@wrd-ThinkPad-X240:~$ " https://pypi.org/project/braian/: “ Skip to main content 🐍⚡️Support Python for everyone by grabbing a 30% discount on PyCharm—ALL proceeds go to the Python Software Foundation. Offer ends soon, so grab it today! GET 30% OFF PYCHARM PyPI Search PyPI braian 1.0.5 pip install braian Latest version Released: Mar 11, 2025 A python library for easy navigation, visualisation, and analysis of whole-brain quantification data. Project description Project details Release history Download files Project description braian logo BraiAn PyPI - Version status-badge Installation Once you are in an active python>=3.11,<3.14 environment, you can run: python3 -m pip install braian Citing If you use BraiAn in your work, please cite the paper below, currently in pre-print: Chiaruttini, N., Castoldi, C. et al. ABBA, a novel tool for whole-brain mapping, reveals brain-wide differences in immediate early genes induction following learning. bioRxiv (2024). https://doi.org/10.1101/2024.09.06.611625 Building Prerequisites git Poetry or venv/conda/pyenv/pyenv-win to manage dependencies. Windows instructions assume that you configured the PATH and PATHEXT variables with its command-line program location (e.g. git or pip). If you can't/didn't, you can juxtapose the path-to-exectuable to the respective commands (e.g., C:\Python311\python instead of python). If you don't know how, we recommend using Scoop. Step 1: clone the repository git clone https://codeberg.org/SilvaLab/BraiAn.git /path/to/BraiAn Step 2: install with Poetry cd /path/to/BraiAn poetry install # --with docs, to install documentation dependencies # --with dev, to install basic dependencies to work on ipython Poetry will automatically create a virtual environment in which it installs all the dependencies. If, instead, you want to manage the environment yourself, Poetry uses the one active during the installation. with pip Requires python>=3.11,<3.14. pip install -e /path/to/BraiAn Note: installing with pip doesn't assure to install the same version of the dependencies used by developers to run and test braian. Help Installing packages Uploading packages User guide Project name retention FAQs About PyPI PyPI Blog Infrastructure dashboard Statistics Logos & trademarks Our sponsors Contributing to PyPI Bugs and feedback Contribute on GitHub Translate PyPI Sponsor PyPI Development credits Using PyPI Terms of Service Report security issue Code of conduct Privacy Notice Acceptable Use Policy Status: All Systems Operational Developed and maintained by the Python community, for the Python community. Donate today! "PyPI", "Python Package Index", and the blocks logos are registered trademarks of the Python Software Foundation. © 2025 Python Software Foundation Site map AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page ”
12-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值