推荐学习书目
› 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
tuoov
V2EX  ›  Python

求助: Python 用 wsgiref 写数据接口

  •  
  •   tuoov · Nov 20, 2020 · 3041 views
    This topic created in 2173 days ago, the information mentioned may be changed or developed.
    客户端 post 一个带 json 数据的请求,我的处理函数是这样的:

    def save_task(environ, start_response):
    start_response('200 OK', [ ('Content-type','text/plain')])
    text=environ['wsgi.input'].read(int(environ.get("CONTENT_LENGTH", 0)))
    postData=json.loads(text.decode('utf-8'))
    resp = json.dumps({"code ": 0,"message": "成功"},ensure_ascii=False)
    return resp.encode('utf-8')

    现在拿到的数据是没问题的,但返回给客户端的响应状态码永远是 500,程序也不抛出任何异常。请问是哪里出了问题?
    6 replies  •  2020-11-21 19:49:22 +08:00
    est
    est
    Nov 20, 2020❤️2
    1
    是谁让你直接拿 wsgiref 开撸接口的,你去怼它。
    GodFastion
    GodFastion
    Nov 20, 2020 via Android
    2
    python 写接口有更好的选择吧,fastapi flask 都比较轻量好用吧,
    ynkkdev
    ynkkdev
    Nov 20, 2020
    3
    这就好比你来到了一个米其林餐厅,却选择了自己煮泡面吃
    MoYi123
    MoYi123
    Nov 20, 2020
    4
    return resp.encode('utf-8') 改成 return [resp.encode('utf-8')] 即可
    julyclyde
    julyclyde
    Nov 21, 2020
    5
    it must return a write(body_data) callable
    这句话咋理解?
    julyclyde
    julyclyde
    Nov 21, 2020
    6
    抱歉上面那句是我看错章节了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1293 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 21:08 · PVG 05:08 · LAX 13:08 · JFK 16:08
    ♥ Do have faith in what you're doing.