推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
PPTX
V2EX  ›  Python

Python 怎么监听多个端口的 http 消息?

  •  
  •   PPTX · Apr 4, 2020 · 6289 views
    This topic created in 2406 days ago, the information mentioned may be changed or developed.

    端口可能会动态变化

    17 replies  •  2020-04-05 16:29:11 +08:00
    black11black
    black11black
    Apr 4, 2020
    1
    奇怪的需求,不知道原生有没有人做这种。aiohttp 有抽象 http 协议接口,你自己写重构逻辑层的话可以实现多监听。不想大动干戈的最简单方法是找一些业务的 tricky point,deamon 线程随着端口变化动态绑定服务端口。
    ClericPy
    ClericPy
    Apr 4, 2020
    2
    丢给 upstream 那头 nginx 负责转发?
    est
    est
    Apr 4, 2020 via Android
    3
    如果有别的语言的例子,抄一个
    lithbitren
    lithbitren
    Apr 4, 2020 via iPhone
    4
    另写一个进程管理,多进程启动多个服务,不需要的服务可以直接把进程扬了。
    cz5424
    cz5424
    Apr 4, 2020
    5
    百度一下抓包软件的实现方法
    SingeeKing
    SingeeKing
    Apr 4, 2020
    6
    核心逻辑起一个项目 A,网络相关的起一个项目 B,再来一个项目 C 专门进行网络项目的启动管理。

    然后网络相关的都转发到 A 进行,C 专门用来启动多个 B
    PPTX
    PPTX
    OP
    Apr 4, 2020
    7
    我表述的有点问题,程序需要接收多个端口的 http 消息,且端口数量和端口号可能动态变化
    PPTX
    PPTX
    OP
    Apr 4, 2020
    8
    @black11black 我表述的有点问题,程序需要接收多个端口的 http 消息,且端口数量和端口号可能动态变化
    @cz5424
    @ClericPy
    @lithbitren
    @SingeeKing
    @ClericPy
    @est
    chibupang
    chibupang
    Apr 4, 2020 via iPhone
    9
    如果是 Java 可以通过 NIO Selector 注册事件来实现.....Python 打扰了....
    NYGK
    NYGK
    Apr 4, 2020
    10
    最直接的办法 multiprocessing 开多进程,每个进程监听一个;
    如果需要需要变更端口监听可以创建新进程或者 kill 原有进程。这方法最直接但是有点蠢。
    ipwx
    ipwx
    Apr 4, 2020
    11
    上 aio 啊。所有 server socket 的 coroutine 把任务扔给 executor 不就行了。监听和执行分割开来就行了,以后你想上非 http 也易如反掌。
    tempdban
    tempdban
    Apr 5, 2020 via Android
    12
    raw socket
    FINDNOMORE
    FINDNOMORE
    Apr 5, 2020
    13
    如果只是验证的话,自己用 socket 套接字手动绑定接受就可以了
    keenkiller
    keenkiller
    Apr 5, 2020 via Android
    14
    Go 的话很简单…python 你只能多进程了
    black11black
    black11black
    Apr 5, 2020
    15
    @ipwx aiohttp 的 serve 默认阻塞的,相当于接管事件循环。你要从 socket 搭的话相当于重新实现 http 协议。
    ipwx
    ipwx
    Apr 5, 2020
    16
    @black11black asyncio 的 executor 支持线程池啊。aiohttp 接受到的请求,然后用 asyncio Queue 把 task 塞进去,阻塞的东西算完,然后再把结果在 http 的 coroutine 上通过 await 弄回来不久行了???整个过程不会阻塞 http 的事件循环。
    julyclyde
    julyclyde
    Apr 5, 2020
    17
    基本上可以猜到是需求有问题

    多开几个 listener 呗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1688 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 17:15 · PVG 01:15 · LAX 09:15 · JFK 12:15
    ♥ Do have faith in what you're doing.