V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
Richard14
V2EX  ›  Python

Python3.5 版本应该如何快速搭建 web 服务?

  •  
  •   Richard14 · 2021-08-20 14:39:29 +08:00 · 2844 次点击
    这是一个创建于 951 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,生产环境有一套嵌入式 python,基于 3.5 版本不能修改。想要通过 web 服务进行控制,而且最好不依赖第三方库,有什么代码可以快速实现一个基础的基于 http (或者 tcp )进行远程调用的服务吗?

    第 1 条附言  ·  2021-08-21 10:40:41 +08:00

    贴个条更新一下目前的信息:

    1、aiohttp可以在3.5运行,不好用

    2、bottle我试了,但是因为内核线程管理策略的原因,平台导致效率应该会比异步低很多,不过应该也堪用。bottle的问题是,本身没法支持ssl协议,http在大量通信的情况下可能出现错误。。。试了试安装旧版本的werkzeug和openssl这些,openssl我没有安装成功

    16 条回复    2021-08-22 18:28:31 +08:00
    AoEiuV020
        1
    AoEiuV020  
       2021-08-20 14:55:32 +08:00
    python3 有个自带的 http.server,应该可以简单实现 http 服务器端,
    SbloodyS
        2
    SbloodyS  
       2021-08-20 15:04:37 +08:00
    Richard14
        3
    Richard14  
    OP
       2021-08-20 15:19:25 +08:00
    @AoEiuV020
    @SbloodyS
    有基于 aiohttp 的代码吗,io 应该可以降低很多开销
    0bit
        4
    0bit  
       2021-08-20 15:24:04 +08:00
    Python 自带 RPC,可以试试,不过好多年没用过这种方案了,感觉有点怪
    yucongo
        5
    yucongo  
       2021-08-20 15:30:15 +08:00
    python -m http.server

    服务端口:0.0.0.0:8080

    帮助:python -m http.server --help
    abersheeran
        6
    abersheeran  
       2021-08-20 15:31:13 +08:00
    如果你不想要第三方依赖,标准库里的 XML RPC 是你唯一的选择了。

    另外,aiohttp 不支持 python3.5
    renmu123
        7
    renmu123  
       2021-08-20 15:56:55 +08:00 via Android
    不想要依赖就只能手撸了
    keepeye
        8
    keepeye  
       2021-08-20 16:09:57 +08:00
    @abersheeran 3.5.6 正在用 aiohttp
    among
        9
    among  
       2021-08-20 16:34:25 +08:00
    bottle,就一个 py 文件。
    debuggerx
        10
    debuggerx  
       2021-08-20 17:05:01 +08:00
    试试 bottle 这个框架?
    http://www.bottlepy.org/docs/dev/
    单文件即可
    cloverstd
        11
    cloverstd  
       2021-08-20 18:04:03 +08:00
    此处就体现了 Go 的优势了
    Kobayashi
        12
    Kobayashi  
       2021-08-20 18:15:48 +08:00
    3.5 就别考虑异步了,最好 3.7 起步。
    abersheeran
        13
    abersheeran  
       2021-08-20 21:04:41 +08:00
    @keepeye 上古版本的 aiohttp ?你用的那版本连接泄露的 BUG 修了吗
    SingeeKing
        14
    SingeeKing  
       2021-08-20 21:53:48 +08:00 via iPhone
    最好不依赖第三方库 aiohttp 难道不是第三方
    Richard14
        15
    Richard14  
    OP
       2021-08-21 09:31:12 +08:00
    @SingeeKing 打错了,脑袋里想的是 asyncio,打成 aiohttp 了
    Leon6868
        16
    Leon6868  
       2021-08-22 18:28:31 +08:00
    你可以参考 pydoc 的实现方法,用 python 自带的 http 模块实现一个基础的服务器。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1037 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:23 · PVG 06:23 · LAX 15:23 · JFK 18:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.